Suppose I have a Java program Test.class
. I want to measure its execution time. I wrote a wrapper to do this as below:
class RunTest {
public static void main(String[] args) {
long sum = 0;
int iterations = 20;
int warmupNum = 10;
for(int i=0; i<iterations; i++){
long start = System.nanoTime();
Test.main(args);
long end = System.nanoTime();
if( i > warmupNum )
sum += end - start;
}
System.out.println("ave: "+sum/(iterations-warmupNum));
}
}
Here how to choose warmupNum
, the larger the better? How large is enough? Is this a "standard/common" way to measure Java program's performance?
It is better to use Caliper than creating your own micro-benchmark utility.
How do I write a correct micro-benchmark in Java?