7

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?

JackWM
  • 10,085
  • 22
  • 65
  • 92
  • 3
    10,000 is the default value before the JIT starts doing its job. You might be interested in reading [this post](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java). – assylias Aug 16 '12 at 17:37
  • @assylias 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. – JackWM Aug 16 '12 at 17:39
  • 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. – assylias Aug 16 '12 at 17:40
  • [this gives more info](http://stackoverflow.com/a/4626779/829571) - 10000 is in server mode - it seems to be 1500 in client mode. – assylias Aug 16 '12 at 17:43
  • @JackWM Accept my answer if I've answered your question. – Aravind Yarram Sep 21 '13 at 15:44

1 Answers1

4

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

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

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327