Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to time Java program execution speed

Tags:

java

How do you time the execution of a java program? I'm not sure what class I should use to do this.

I'm kinda looking for something like:

// Some timer starts here for (int i = 0; i < length; i++) {   // Do something } // End timer here  System.out.println("Total execution time: " + totalExecutionTime); 
like image 521
George Mic Avatar asked Apr 03 '10 22:04

George Mic


People also ask

How do you calculate execution 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. The nanoTime() method returns the current time in nano seconds.

What happens at run time in Java?

Runtime is the final phase of the program lifecycle in which the machine executes the program's code. When the source code of the program is being edited. This phase includes bug fixing, refactoring, and adding new features.


1 Answers

final long startTime = System.currentTimeMillis(); for (int i = 0; i < length; i++) {   // Do something } final long endTime = System.currentTimeMillis();  System.out.println("Total execution time: " + (endTime - startTime)); 
like image 172
bua Avatar answered Sep 30 '22 07:09

bua