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

1

8

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?

Countdown answered 16/8, 2012 at 17:36 Comment(5)
10,000 is the default value before the JIT starts doing its job. You might be interested in reading this post.Subkingdom
@Subkingdom You are saying the 10000 is the number of repetitions for optimizing a method, right? Here, I want to know of warmup iterations of program to get a stable execution time.Countdown
yes - the JIT will start compiling a method after it's been called 10,000 times. You can change that value with the parameters you pass to the JVM but I can't find it right now.Subkingdom
this gives more info - 10000 is in server mode - it seems to be 1500 in client mode.Subkingdom
@Countdown Accept my answer if I've answered your question.Selfinductance
S
4

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

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

Selfinductance answered 16/8, 2012 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.