How To Mensurate Elapsed Execution Fourth Dimension Inward Coffee - Boundary Framework's Stopwatch Example

There are two ways to mensurate elapsed execution fourth dimension inwards Java either past times using System.currentTimeinMillis()or past times using  System.nanoTime(). These 2 methods tin dismiss hold out used to mensurate elapsed or execution fourth dimension betwixt 2 method calls or trial inwards Java. Calculating elapsed time is ane of the initiative off affair Java programmer exercise to uncovering 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 amongst System.currentTimeInMillis()which is in that location from outset piece a novel version of to a greater extent than precise fourth dimension standard utility System.nanoTime is introduced inwards Java 1.5, along amongst several novel features inwards linguistic communication similar Generics, Enum types, machine boxing in addition to variable arguments or varargs. You tin dismiss purpose whatever of them to mensurate execution fourth dimension of method inwards Java. Though its amend to purpose System.nanoTime() for to a greater extent than precise standard of fourth dimension intervals.

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


Java Program instance to mensurate execution fourth dimension inwards Java

two ways to mensurate elapsed execution fourth dimension inwards Java How to Measure Elapsed Execution Time inwards Java - Spring Framework's StopWatch Examplethread safe in addition to should non hold out shared inwards multi-threading surround in addition to its documentation clearly says that this is to a greater extent than suitable inwards evolution in addition to seek out surround for basic functioning standard rather performing fourth dimension calculation inwards production environment.

import org.springframework.util.StopWatch;

/**
 * Simple Java Program to mensurate 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 v in addition to 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 pick inwards damage of availability piece later JDK 1.5 System.nanoTime is peachy for measuring elapsed fourth dimension equally it is to a greater extent than accurate in addition to uses accurate organisation clock in addition to tin dismiss measure upto nano instant precision. piece if you lot are using whatever of Java opened upwards root library mentioned above, to a greater extent than oft than non Spring than StopWatch is too a amend pick precisely equally I said before StopWatch is non thread-safe in addition to should solely hold out used inwards evolution in addition to seek out environment. Just similar SimpleDateFormat is non thread-safe in addition to you lot tin dismiss purpose ThreadLocal to exercise per thread SimpleDateFormat you lot tin dismiss exercise the same affair amongst StopWatch equally well. But I don’t intend StopWatch is heavy object similar SimpleDateFormat.

That's all on how to mensurate elapsed fourth dimension or execution fourth dimension inwards Java. Get yourself inwards habit of measuring functioning in addition to execution fourth dimension of  of import slice of codes peculiarly methods or loops which executes to a greater extent than or less of the time. Those are the places where an small-scale optimization inwards code trial 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 contrary String inwards Java using recursion

Komentar

Postingan populer dari blog ini

Difference Betwixt Struts Validatorform Vs Validatoractionform - Interview Question

How To Convert Inputstream To Byte Array Inwards Coffee - Two Examples

Difference Betwixt Fileinputstream Together With Filereader Inwards Coffee | Inputstream Vs Reader