Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many runs of Java program do we need to warm-up the JVM?

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?

like image 203
JackWM Avatar asked Aug 16 '12 17:08

JackWM


People also ask

How do you warm up JVM?

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.

What happens when JVM starts?

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[]) .


1 Answers

It is better to use Caliper than creating your own micro-benchmark utility.

How do I write a correct micro-benchmark in Java?

like image 61
Aravind Yarram Avatar answered Sep 18 '22 23:09

Aravind Yarram