Tom
Tom

Reputation: 26829

Java execution time affected by string formatting?

I was doing some performance test (sum up around a million integers) with the following simple code:

final int[] array = new int[1024 * 1024];

// populate values
for (int i = 0; i < array.length; i++) {
    array[i] = i % 100;
}

long start, end, sum = 0;

start = System.nanoTime();

// calculate sum
for (int i : array) {
    sum += i;
}

end = System.nanoTime() - start;

System.out.println(end);

Average execution time was around 1.8ms.

Then I decided to change test results presentation. I replaced System.out.println(end) with the following statement (formatted string) :

System.out.printf("Time in %dns, sum: %d\n", end, sum);

And it was very surprising when I noticed that my average execution time was around 2.8ms. It is only a millisecond more (not a big deal) but on the other hand it is about 50% slower. More to that, test results presentation (and formatting) is located after end time measure is taken, so theoretically it should not affect average execution time.

Do you know why average execution time takes more time after adding System.out.printf statement? Is it caused by reordering (formatting executed before assigning value to the end variable)?

Upvotes: 0

Views: 133

Answers (1)

Stephen C
Stephen C

Reputation: 719119

I think that you cannot draw any conclusions from this. Basically, your benchmark is flawed:

  1. It does not take the necessary steps to warm up the JVM before it captures the measurement.

  2. The loop that does the "work" that you are measuring can be optimized away. The compiler (javac or the JIT compiler) can deduce that the sum is never used, and therefore not calculate it.

Either of these flaws could distort the results you are seeing.


For what it is worth, if the benchmark was properly written, then the fancy formatting should not contribute to the measured time at all.

Upvotes: 5

Related Questions