iamprem
iamprem

Reputation: 636

System.nanoTime() in Java gives different running time for same inputs

I used the following code to measure the running time of my program,

Code:

public static void main(String[] args) {
    String a = "agtacgtcatacgtctagtactacgttca";
    String b = "gtatccctagactsgtatcatacgtctat";
    long startTime = System.nanoTime();
    //This method call computes the Longest Common Sequence of the given 2 strings.
    System.out.println(LCS.Linear.computeLCS(a, b));
    long endTime = System.nanoTime();
    System.out.println("Took "+(endTime - startTime) + " ns");
}

Sample Outputs for same input:

Run 1:

gtacctagctgtactacgtta
Took 1971471 ns

Run 2:

gtacctagctgtactacgtta
Took 2242336 ns

Why is the difference in running time each time?

How to find the actual running time of that method call?

Upvotes: 0

Views: 592

Answers (2)

aioobe
aioobe

Reputation: 421310

Those running times are in the magnitude of 2 milliseconds! You can't expect them to be the same for two separate runs. From the documentation:

This method provides nanosecond precision, but not necessarily nanosecond accuracy.

Micro benchmarks like these are extremely hard to get accurate figures for. You have several sources of nondeterminism:

  • other processes on your computer
  • JVM warmup
  • JIT compilation
  • garbage collection

to name a few. You should at least try to run your algorithm 10.000 times or so and take the average.

Further reading:

Upvotes: 9

Philipp Claßen
Philipp Claßen

Reputation: 44019

It is difficult to reliable measure time. I think it is less a Java problem, but more a question how to can ensure that the system does nothing beside executing your code.

I think the only real solution is to replace the measure but a series of measurement and use statistics. There are some frameworks for microbenchmarking that you should investigate. For instance:

To make matters worse, even the JIT optimization of the JVM itself is non-deterministic, so when you restart the JVM and measure again, in general, the optimized code itself will differ.

There is a great talk from Joshua Bloch about the topic: Performance Anxiety – on Performance Unpredictability, Its Measurement and Benchmarking (link to slides)

Upvotes: 3

Related Questions