∟"int" Multiply and Assignment: 17 Nanoseconds per Step
This section provides a tutorial example on how to perform benchmark tests to find out how long a multipication and assignment loop will take to run. The answer is 17 nanoseconds per step.
The next test is to measure the multiply operation performance.
Here is the test method:
/**
* BenchmarkTestInt.java
* Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
*/
class BenchmarkTestInt {
...
// Test method for a multiply operation in a loop
public static int multiply(int steps, BenchmarkRunner runner) {
int x = 0;
int i = 0;
int last = steps;
runner.startTimer();
for (i=0; i<last; i++) {
x = i*3;
}
runner.stopTimer();
return x;
}
}
The loop runs 17 nanoseconds per step with a multiplication operation and an assignment operation.
Clearly, the execution time of a multiplication operation, i*3, is about 3 nanoseconds in interpreted-only mode.
This conclusion can be arrived from test results of 10000, 100000 and 1000000 steps and minus
the assignment loop execution time: 17 - 14 = 3 nanoseconds.
This result is a surprise to me. Comparing with the previous add operation test,
i*3 takes the same execution time as i+i. I thought a multiplication operation is always slower that an add operation.
This is probably related the constant 3 used in the multiplication.