Question

I would like to compare the speed performance (if there were any) from the two readDataMethod() as I illustrate below.

private void readDataMethod1(List<Integer> numbers) {
    final long startTime = System.nanoTime();
    for (int i = 0; i < numbers.size(); i++) {
        numbers.get(i);
    }
    final long endTime = System.nanoTime();
    System.out.println("method 1 : " + (endTime - startTime));
}

private void readDataMethod2(List<Integer> numbers) {
    final long startTime = System.nanoTime();
    int i = numbers.size();
    while (i-- > 0) {
        numbers.get(i);
    }
    final long endTime = System.nanoTime();
    System.out.println("method 2 : " + (endTime - startTime));
}

Most of the time the result I get shows that method 2 has "lower" value.

   Run         readDataMethod1    readDataMethod2

     1                  636331             468876
     2                  638256             479269
     3                  637485             515455
     4                  716786             420756

Does this test prove that the readDataMethod2 is faster than the earlier one ?

Was it helpful?

Solution

Does this test prove that the readDataMethod2 is faster than the earlier one ?

You are on the right track in that you're measuring comparative performance, rather than making assumptions.

However, there are lots of potential issues to be aware of when writing micro-benchmarks in Java. I would recommend that you read How do I write a correct micro-benchmark in Java?

OTHER TIPS

In the first one, you are calling numbers.size() for each iteration. Try storing it in a variable, and check again.

The reason because of which the second version runs faster is because you are calling numbers.size() on each iteration. Replacing it by storing in a number would make it almost the same as the first one.

Does this test prove that the readDataMethod2 is faster than the earlier one ?

As @aix says, you are on the right track. However, there are a couple of specific issues with your methodology:

  • It doesn't look like you are "warming up" the JVM. Therefore it is conceivable that your figures could be distorted by startup effects (JIT compilation) or that none of the code has been JIT compiled.

  • I'd also argue that your runs are doing too little work. A 500000 nanoseconds, is 0.0005 seconds, and that's not much work. The risk is that "other things" external to your application could be introducing noise into the measurements. I'd have more confidence in runs that take tens of seconds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top