How to measure time in java

Java Time Measurement

Measuring time in Java is easiest to do with the System.currentTimeMillis() method.

What you do is that you get the time before and after the operation you want to measure, and calculate the time difference. Here is an example:

long startTime = System.currentTimeMillis(); callOperationToTime(); long endTime = System.currentTimeMillis(); long totalTime = endTime - startTime;

The variable totalTime will now contain the total time it took to execute the callOperationToTime() method.

Repeat the Operation

Because System.currentTimeMillis() does not return the time that accurately, it is a good idea to execute the operation to measure more than once. Perhaps 10, 100 or 1.000 times. Maybe even more. That way inaccuracy caused by large grained time values (values that does not change every millisecond) is leveled out.

Another good reason to repeat the operation to measure a lot of times is to allow the Java virtual machine to load the classes containing the code, JIT-compile it, and perhaps even optimize it.

A Timer class

The calculations listed earlier in this text are rather trivial yet tedious to do, and could be encapsulated in a Timer class. Here is a sketch:

public class Timer < private long startTime = 0; private long endTime = 0; public void start()< this.startTime = System.currentTimeMillis(); >public void end() < this.endTime = System.currentTimeMillis(); >public long getStartTime() < return this.startTime; >public long getEndTime() < return this.endTime; >public long getTotalTime() < return this.endTime - this.startTime; >>

Here is an example of how to use it:

Timer timer = new Timer(); timer.start(); callOperationToTime(); timer.end(); long totalTime = timer.getTotalTime();

Источник

Читайте также:  Php can parse int

How To Measure Elapsed Time in Java

Learn to calculate execution time or measure elapsed time of a program or some Java statements using various techniques pre and post Java 8 release.

1. Measuring Elapsed Time since Java 8

If we’re using Java 8 – we can try the new java.time.Instant and java.time.Duration classes. Below Java 8, proceed to the next method down in the article.

To get the elapsed execution time in different time units, use the following method. It measures the duration between two Instants. And Instant represents the time elapsed since the epoch.

long timeElapsed = Duration.between(startInstant, finishInstant).toMillis();
import java.text.ParseException; import java.util.concurrent.TimeUnit; public class Main < public static void main(String[] args) throws ParseException < Instant start = Instant.now(); //Measure execution time for this method methodToTime(); Instant finish = Instant.now(); long timeElapsed = Duration.between(start, finish).toMillis(); //in millis >private static void methodToTime() < try < TimeUnit.SECONDS.sleep(3); >catch (InterruptedException e) < e.printStackTrace(); >> >

This is the most recommended solution to measure elapsed time in Java. It provides nanoseconds level precision of elapsed time between two measurements. It is the most preferred approach to calculate thread execution time in Java.

import java.text.ParseException; import java.util.concurrent.TimeUnit; public class Main < public static void main(String[] args) throws ParseException < long startTime = System.nanoTime(); methodToTime(); //Measure execution time for this method long endTime = System.nanoTime(); long durationInNano = (endTime - startTime); //Total execution time in nano seconds //Same duration in millis long durationInMillis = TimeUnit.NANOSECONDS.toMillis(durationInNano); //Total execution time in nano seconds System.out.println(durationInNano); System.out.println(durationInMillis); >private static void methodToTime() < try < TimeUnit.SECONDS.sleep(3); >catch (InterruptedException e) < e.printStackTrace(); >> >
3000076434 //More precise 3000

If you are not too concerned about nano level precision, or unfortunately still stuck in legacy Java versions – You shall be using System.currentTimeMillis() method.

import java.text.ParseException; import java.util.concurrent.TimeUnit; public class Main < public static void main(String[] args) throws ParseException < long startTime = System.currentTimeMillis(); methodToTime(); //Measure execution time for this method long endTime = System.currentTimeMillis(); long duration = (endTime - startTime); //Total execution time in milli seconds System.out.println(duration); >private static void methodToTime() < try < TimeUnit.SECONDS.sleep(3); >catch (InterruptedException e) < e.printStackTrace(); >> >

We can convert the above time in Millis to other time units such as hours, minutes and seconds to measure execution time in corresponding time units.

Источник

How to measure elapsed time in Java?

The currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.

Example

public class Example < public void test()< int num = 0; for(int i=0; i> public static void main(String args[]) < //Start time long begin = System.currentTimeMillis(); //Starting the watch new Example().test(); //End time long end = System.currentTimeMillis(); long time = end-begin; System.out.println(); System.out.println("Elapsed Time: "+time +" milli seconds"); >>

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: 4 milli seconds

Using the nanoTime() method

The nanoTime() method returns the current time in nano seconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.

Example

public class Example < public void test()< int num = 0; for(int i=0; i> public static void main(String args[]) < //Start time long begin = System.nanoTime(); //Starting the watch new Example().test(); //End time long end = System.nanoTime(); long time = end-begin; System.out.println(); System.out.println("Elapsed Time: "+time); >>

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: 1530200

Using the Instant class

The now() method of the Instant class returns the current time and the Duration.between() methods returns the difference between the given two time values to get the elapsed time retrieve the time values before and after the execution of the desired method and retrieve the duration using the Duration.between() method.

Example

import java.time.Duration; import java.time.Instant; public class Example < public void test()< int num = 0; for(int i=0; i> public static void main(String args[]) < //Starting time Instant start = Instant.now(); new Example().test(); //End time Instant end = Instant.now(); long time = Duration.between(start, end).toMillis(); System.out.println(); System.out.println(time+" Milli seconds"); >>

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, 3 Milli seconds

Using the StopWatch class

The Apache commons library provides a class known as Stopwatch to it provides the start() stop() and getTime() methods to find the time taken for the execution of a method.

Following is the maven file of this package −

 org.apache.commons commons-lang3 3.7 

Example

import org.apache.commons.lang3.time.StopWatch; public class Example < public void test()< int num = 0; for(int i=0; i> public static void main(String args[]) < //Instantiating the StopWatch class StopWatch obj = new StopWatch(); //Starting the watch obj.start(); new Example().test(); //Stopping the watch obj.stop(); System.out.println(); System.out.println("Elapsed Time: "+obj.getTime() +" milli seconds"); >>

Output

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: 1 milli seconds

Источник

Java Time Measurement

Measuring time in Java is easiest to do with the System.currentTimeMillis() method.

What you do is that you get the time before and after the operation you want to measure, and calculate the time difference. Here is an example:

long startTime = System.currentTimeMillis(); callOperationToTime(); long endTime = System.currentTimeMillis(); long totalTime = endTime - startTime;

The variable totalTime will now contain the total time it took to execute the callOperationToTime() method.

Repeat the Operation

Because System.currentTimeMillis() does not return the time that accurately, it is a good idea to execute the operation to measure more than once. Perhaps 10, 100 or 1.000 times. Maybe even more. That way inaccuracy caused by large grained time values (values that does not change every millisecond) is leveled out.

Another good reason to repeat the operation to measure a lot of times is to allow the Java virtual machine to load the classes containing the code, JIT-compile it, and perhaps even optimize it.

A Timer class

The calculations listed earlier in this text are rather trivial yet tedious to do, and could be encapsulated in a Timer class. Here is a sketch:

public class Timer < private long startTime = 0; private long endTime = 0; public void start()< this.startTime = System.currentTimeMillis(); >public void end() < this.endTime = System.currentTimeMillis(); >public long getStartTime() < return this.startTime; >public long getEndTime() < return this.endTime; >public long getTotalTime() < return this.endTime - this.startTime; >>

Here is an example of how to use it:

Timer timer = new Timer(); timer.start(); callOperationToTime(); timer.end(); long totalTime = timer.getTotalTime();

Источник

Оцените статью