How To Stair Out Elapsed Execution Fourth Dimension Inward Coffee - Restrict Framework's Stopwatch Example

There are two ways to stair out elapsed execution fourth dimension inwards Java either past times using System.currentTimeinMillis()or past times using  System.nanoTime(). These 2 methods tin live on used to stair out elapsed or execution fourth dimension betwixt 2 method calls or lawsuit inwards Java. Calculating elapsed time is ane of the maiden of all affair Java programmer exercise to detect out how many seconds or millisecond a method is taking to execute or how much fourth dimension a detail code block is taking. Most of Java programmer are familiar alongside System.currentTimeInMillis()which is at that topographic point from commencement spell a novel version of to a greater extent than precise fourth dimension touchstone utility System.nanoTime is introduced inwards Java 1.5, along alongside several novel features inwards linguistic communication similar Generics, Enum types, machine boxing together with variable arguments or varargs. You tin purpose whatsoever of them to stair out execution fourth dimension of method inwards Java. Though its improve to purpose System.nanoTime() for to a greater extent than precise touchstone of fourth dimension intervals.

In this Java programming tutorial nosotros volition run into a uncomplicated Java programme to stair out execution fourth dimension past times using System.nanoTime() together with Spring framework’s StopWatch utility class. This article is inwards continuation of my ship service on roofing telephone commutation Java concepts similar How to compare String inwards Java, How to write equals method inwards Java correctly together with 4 ways to loop HashMap inwards Java. If you lot haven’t read them already you lot may detect them useful.


Java Program instance to stair out execution fourth dimension inwards Java

two ways to stair out elapsed execution fourth dimension inwards Java How to Measure Elapsed Execution Time inwards Java - Spring Framework's StopWatch Examplethread safe together with should non live on shared inwards multi-threading environs together with its documentation clearly says that this is to a greater extent than suitable inwards evolution together with examine environs for basic functioning touchstone rather performing fourth dimension calculation inwards production environment.

import org.springframework.util.StopWatch;

/**
 * Simple Java Program to stair out elapsed execution fourth dimension inwards Java
 * This Java Program shows 2 ways for measuring fourth dimension inwards Java, past times using System.nanoTime() which was
 * added inwards Java five together with StopWatch which is a utility cast from Spring Framework.
 */

public class MeasureTimeExampleJava {

 

    public static void main(String args[]) {
     
        //measuring elapsed fourth dimension using System.nanoTime
        long startTime = System.nanoTime();
        for(int i=0; i< 1000000; i++){
            Object obj = new Object();
        }
        long elapsedTime = System.nanoTime() - startTime;
     
        System.out.println("Total execution fourth dimension to exercise 1000K objects inwards Java inwards millis: "
                + elapsedTime/1000000);
     
        //measuring elapsed fourth dimension using Spring StopWatch
        StopWatch picket = new StopWatch();
        watch.start();
        for(int i=0; i< 1000000; i++){
            Object obj = new Object();
        }
        watch.stop();
        System.out.println("Total execution fourth dimension to exercise 1000K objects inwards Java using StopWatch inwards millis: "
                + watch.getTotalTimeMillis());
    }  
     
}

Output:
Total execution fourth dimension to exercise 1000K objects inwards Java inwards millis: 18
Total execution fourth dimension to exercise 1000K objects inwards Java using StopWatch inwards millis: 15


Which ane should you lot purpose for measuring execution fourth dimension inwards Java

It depends which options are available to you, if you lot are working inwards below JDK 1.5 version than System.currentTimeInMillis() is the best choice inwards price of availability spell later JDK 1.5 System.nanoTime is neat for measuring elapsed fourth dimension equally it is to a greater extent than accurate together with uses accurate organization clock together with tin measure upto nano instant precision. spell if you lot are using whatsoever of Java opened upwardly rootage library mentioned above, generally Spring than StopWatch is likewise a improve choice exactly equally I said before StopWatch is non thread-safe together with should alone live on used inwards evolution together with examine environment. Just similar SimpleDateFormat is non thread-safe together with you lot tin purpose ThreadLocal to exercise per thread SimpleDateFormat you lot tin exercise the same affair alongside StopWatch equally well. But I don’t mean value StopWatch is heavy object similar SimpleDateFormat.

That's all on how to stair out elapsed fourth dimension or execution fourth dimension inwards Java. Get yourself inwards habit of measuring functioning together with execution fourth dimension of  of import slice of codes peculiarly methods or loops which executes well-nigh of the time. Those are the places where an small-scale optimization inwards code termination inwards bigger functioning gain.

Further Reading
Spring Framework 5: Beginner to Guru
The Complete Java MasterClass
Core Java, Volume II--Advanced Features
Java Program to opposite String inwards Java using recursion

0 Response to "How To Stair Out Elapsed Execution Fourth Dimension Inward Coffee - Restrict Framework's Stopwatch Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel