This section provides a tutorial example on how to perform benchmark tests to find out how long each step will take to run assignment loop - 14 nanoseconds per step.
Using the empty loop execution time obtained from the previous tutorial as a baseline, I can do more interesting tests now.
This time I want to find out how long it will take to do an assignment operation in interpreted-only mode.
Here is my test method for this:
/**
* BenchmarkTestLong.java
* Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
*/
class BenchmarkTestLong {
...
// Test method for an assignment operation in a loop
public static long assignment(int steps, BenchmarkRunner runner) {
long x = 0;
long i = 0;
long last = steps;
runner.startTimer();
for (i=0; i<last; i++) {
x = i;
}
runner.stopTimer();
return x;
}
}
The result is very interesting.
Adding an assignment operation in an loop made the loop to run faster!
The assignment, the loop index incrementing and the loop termination checking
take 24 nanoseconds per step.
I have no idea why this assignment loop runs 1 nanoseconds faster than an empty loop per step.