0

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?

iamprem
  • 606
  • 2
  • 9
  • 23
  • 4
    Micro-benchmarking in Java is *much* harder than you think. See [How do I write a correct micro-benchmark in Java?](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Jesper Mar 04 '15 at 19:37

2 Answers2

9

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:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
3

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)

Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239