How to time Java program execution speed
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);
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));
Be aware that there are some issues where System#nanoTime()
cannot be reliably used on multi-core CPU's to record elapsed time ... each core has maintains its own TSC (Time Stamp Counter): this counter is used to obtain the nano time (really it is the number of ticks since the CPU booted).
Hence, unless the OS does some TSC time warping to keep the cores in sync, then if a thread gets scheduled on one core when the initial time reading is taken, then switched to a different core, the relative time can sporadically appear to jump backwards and forwards.
I observed this some time ago on AMD/Solaris where elapsed times between two timing points were sometimes coming back as either negative values or unexpectedly large positive numbers. There was a Solaris kernel patch and a BIOS setting required to force the AMD PowerNow! off, which appeared to solved it.
Also, there is (AFAIK) a so-far unfixed bug when using java System#nanoTime()
in a VirtualBox environment; causing all sorts of bizarre intermittent threading problems for us as much of the java.util.concurrency
package relies on nano time.
See also:
Is System.nanoTime() completely useless? http://vbox.innotek.de/pipermail/vbox-trac/2010-January/135631.html
You get the current system time, in milliseconds:
final long startTime = System.currentTimeMillis();
Then you do what you're going to do:
for (int i = 0; i < length; i++) {
// Do something
}
Then you see how long it took:
final long elapsedTimeMillis = System.currentTimeMillis() - startTime;
You can make use of System#nanoTime()
. Get it before and after the execution and just do the math. It's preferred above System#currentTimeMillis()
because it has a better precision. Depending on the hardware and the platform used, you may otherwise get an incorrect gap in elapsed time. Here with Core2Duo on Windows, between about 0 and ~15ms actually nothing can be calculated.
A more advanced tool is a profiler.
Here are a few ways to find the execution time in Java:
1) System.nanoTime()
long startTime = System.nanoTime();
.....your code....
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println("Execution time in nanoseconds : " + totalTime);
System.out.println("Execution time in milliseconds : " + totalTime / 1000000);
2) System.currentTimeMillis()
long startTime = System.currentTimeMillis();
.....your code....
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Execution time in milliseconds : " + totalTime);
3) Instant.now()
long startTime = Instant.now().toEpochMilli();
.....your code....
long endTime = Instant.now().toEpochMilli();
long totalTime = endTime - startTime;
System.out.println("Execution time in milliseconds: " + totalTime);
or
Instant start = Instant.now();
.....your code....
Instant end = Instant.now();
Duration interval = Duration.between(start, end);
System.out.println("Execution time in seconds: " +interval.getSeconds());
4) Date.getTime()
long startTime = new Date().getTime();
.....your code....
long endTime = new Date().getTime();
long totalTime = endTime - startTime;
System.out.println("Execution time in milliseconds: " + totalTime);