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?
What Is Warming up the JVM. Once class-loading is complete, all important classes (used at the time of process start) are pushed into the JVM cache (native code) – which makes them accessible faster during runtime. Other classes are loaded on a per-request basis.
The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3. 1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]) .
It is better to use Caliper than creating your own micro-benchmark utility.
How do I write a correct micro-benchmark in Java?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With