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

There are two ways to mensurate elapsed execution fourth dimension inward Java either yesteryear using System.currentTimeinMillis()or yesteryear using  System.nanoTime(). These 2 methods tin terminate hold out used to mensurate elapsed or execution fourth dimension betwixt 2 method calls or trial inward Java. Calculating elapsed time is i of the foremost affair Java programmer practise to discovery 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 showtime spell a novel version of to a greater extent than precise fourth dimension mensuration utility System.nanoTime is introduced inward Java 1.5, along amongst several novel features inward linguistic communication similar Generics, Enum types, automobile boxing as well as variable arguments or varargs. You tin terminate role whatsoever of them to mensurate execution fourth dimension of method inward Java. Though its meliorate to role System.nanoTime() for to a greater extent than precise mensuration of fourth dimension intervals.

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


Java Program trial to mensurate execution fourth dimension inward Java

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

import org.springframework.util.StopWatch;

/**
 * Simple Java Program to mensurate elapsed execution fourth dimension inward Java
 * This Java Program shows 2 ways for measuring fourth dimension inward Java, yesteryear using System.nanoTime() which was
 * added inward Java v as well as StopWatch which is a utility shape 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 practise 1000K objects inward Java inward millis: "
                + elapsedTime/1000000);
     
        //measuring elapsed fourth dimension using Spring StopWatch
        StopWatch spotter = 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 practise 1000K objects inward Java using StopWatch inward millis: "
                + watch.getTotalTimeMillis());
    }  
     
}

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


Which i should you lot role for measuring execution fourth dimension inward Java

It depends which options are available to you, if you lot are working inward below JDK 1.5 version than System.currentTimeInMillis() is the best choice inward price of availability spell afterwards JDK 1.5 System.nanoTime is non bad for measuring elapsed fourth dimension every bit it is to a greater extent than accurate as well as uses accurate organisation clock as well as tin terminate measure upto nano instant precision. spell if you lot are using whatsoever of Java opened upwardly origin library mentioned above, generally Spring than StopWatch is too a meliorate choice simply every bit I said before StopWatch is non thread-safe as well as should solely hold out used inward evolution as well as examine environment. Just similar SimpleDateFormat is non thread-safe as well as you lot tin terminate role ThreadLocal to practise per thread SimpleDateFormat you lot tin terminate practise the same affair amongst StopWatch every bit 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 inward Java. Get yourself inward habit of measuring functioning as well as execution fourth dimension of  of import slice of codes peculiarly methods or loops which executes nearly of the time. Those are the places where an pocket-sized optimization inward code final result inward 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 inward Java using recursion

Sumber https://javarevisited.blogspot.com/

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

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel