2

I found that first call of some function take too long time. Here my simple test:

public class MainTest {
    public static void main(String[] args) {
        long k = 0;
        for (int i = 0; i < 10; i++) {
            long start = System.nanoTime();
            k += doWork(i);
            System.out.println(System.nanoTime() - start);
        }
        System.out.println(k);    
    }

    public static long doWork(long var) {
        for (int i = 0; i < 100000; i++) {
            var += i;
        }
        return var;
    }
}

Results:

820521
283961
292514
259442
88952
86100
82965
81539
74126
29651

Can you give me some versions about why it's happening? Or some resources where I can find the answer. It can be connected with JIT, but I'am not sure about this.

I know that nanotime is not correct for testing. I try JMH:

# Warmup Iteration   1: 179791124,395 ops/s
# Warmup Iteration   2: 183962412,435 ops/s
# Warmup Iteration   3: 284320650,805 ops/s
GhostCat
  • 137,827
  • 25
  • 176
  • 248
learp
  • 153
  • 8
  • The call to `nanoTime` probably takes longer than the code you are testing... – assylias May 24 '17 at 09:22
  • In fact I just simplified code. I found this behavior when integrate groovy script in runtime and then just try the code above - same behavior. – learp May 24 '17 at 09:25
  • Possible duplicate of [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Turtle May 24 '17 at 09:27
  • No, of course not. Why you think it's duplicate? I'am not trying to benchmark at all ! – learp May 24 '17 at 09:41
  • It is close to a duplicate because your first problem is that you dont understand how to generate meaningful numbers. Beyond that: I appreciate the quick accept! – GhostCat May 24 '17 at 09:55
  • When you say **meaningful** what you mean? :) I did not try **to generate meaningful numbers**. I interested why I get such results. – learp May 24 '17 at 10:05
  • Well. You intend to draw conclusions from the numbers you generate. You ask "why do I observe xyz"; but what if your *observation* is not significant. But well, I think we all got something out of this. – GhostCat May 24 '17 at 11:30
  • Yeah, I got your point. For these reason I used JMH(just in case). And test above is just simple example, just to explain the problem. – learp May 24 '17 at 11:55

1 Answers1

4

There are two aspects here:

  • Your benchmark is basically flawed and screwed up. You want to read this.
  • Yes, the JIT needs a warm up.

Simply spoken: at runtime, the JIT observes what your code is doing. It applies heuristics that trigger optimizations calls. Thus: don't expected deterministic behavior. It is hard to get to really meaningful numbers.

In order to get more reliable results, you will have to dramatically improve the quality of your "measurement" process.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Tbh, this question is a duplicate of your link, just like for the NPE questions. – Turtle May 24 '17 at 09:28
  • @GhostCat Thanks for the answer. Helped link to warm-up. It's not benchmark at all. I did not try to make **accurate** measurements. I get that behaviour and try to find out why this is so. – learp May 24 '17 at 09:58