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 run a loop with a simple assignment statement in interpreted-only mode.
Here is my test method for this:
/**
* BenchmarkTestInt.java
* Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
*/
class BenchmarkTestInt {
...
// Test method for an assignment operation in a loop
public static int assignment(int steps, BenchmarkRunner runner) {
int x = 0;
int i = 0;
int last = steps;
runner.startTimer();
for (i=0; i<last; i++) {
x = i;
}
runner.stopTimer();
return x;
}
}
The result is very interesting.
Adding an assignment statement in anloop made the loop to run faster!
The assignment, the loop index incrementing and the loop termination checking
take 14 nanoseconds per step.
I have no idea why this assignment loop runs 2 nanoseconds faster than an empty loop per step.