views:

959

answers:

11

In benchmarking some Java code on a Solaris SPARC box, I noticed that the first time I call the benchmarked function it runs EXTREMELY slowly (10x difference):

  • First | 1 | 25295.979 ms
  • Second | 1 | 2256.990 ms
  • Third | 1 | 2250.575 ms

Why is this? I suspect the JIT compiler, is there any way to verify this?

Edit: In light of some answers I wanted to clarify that this code is the simplest possible test-case I could find exhibiting this behavior. So my goal isn't to get it to run fast, but to understand what's going on so I can avoid it in my real benchmarks.

Solved: Tom Hawtin correctly pointed out that my "SLOW" time was actually reasonable. Following this observation, I attached a debugger to the Java process. During the first, the inner loop looks like this:

0xf9037218:     cmp      %l0, 100
0xf903721c:     bge,pn   %icc,0xf90371f4        ! 0xf90371f4
0xf9037220:     nop
0xf9037224:     ld       [%l3 + 92], %l2
0xf9037228:     ld       [%l2 + 8], %l6
0xf903722c:     add      %l6, 1, %l5
0xf9037230:     st       %l5, [%l2 + 8]
0xf9037234:     inc      %l0
0xf9037238:     ld       [%l1], %g0
0xf903723c:     ba,pt    %icc,0xf9037218        ! 0xf9037218

On the following iterations, the loop looks like this:

0xf90377d4:     sub      %l2, %l0, %l3
0xf90377d8:     add      %l3, %l0, %l2
0xf90377dc:     add      %l2, 1, %l4
0xf90377e0:     inc      %l0
0xf90377e4:     cmp      %l0, 100
0xf90377e8:     bl,pn    %icc,0xf90377d8        ! 0xf90377d8

So HotSpot removed memory accesses from the inner loop, speeding it up by an order of magnitude.

Lesson: Do the math! I should have done Tom's calculation myself.

Benchmark Java code:

    private int counter;
    private int nThreads;

    private void measure(String tag) throws Exception {
            MyThread threads[] = new MyThread[nThreads];
            int i;

            counter = 0;

            for (i = 0; i < nThreads; i++)
                    threads[i] = new MyThread();

            long start = System.nanoTime();

            for (i = 0; i < nThreads; i++)
                    threads[i].start();

            for (i = 0; i < nThreads; i++)
                    threads[i].join();

            if (tag != null)
                    System.out.format("%-20s | %-2d | %.3f ms \n", tag, nThreads,
                                     new Double((System.nanoTime() - start) / 1000000.0));
    }
    public MyBench() {
            try {
                    this.nThreads = 1;
                    measure("First");
                    measure("Second");
                    measure("Third");
            } catch (Exception e) {
                    System.out.println("Error: " + e);
            }
    }

    private class MyThread extends Thread {
            public void run() {
                    while (counter < 10000000) {
                            // work
                            for (int j = 0; j < 100; j++)
                                    counter++;
                            counter -= 99;
                    }
            }
    }
+1  A: 

That's an interesting question. I'd suspect the JIT compiler, but these are my numbers:

First                | 1  | 2399.233 ms 
Second               | 1  | 2322.359 ms 
Third                | 1  | 2408.342 ms 

Possibly Solaris is doing something funny with threads; have you tried with nThreads = 10 or so?

Michael Myers
This is a stripped down version of the code, though it exhibits the same behavior. I just wanted to post the simplest possible case here. In other words, I'm not trying to make this specific benchmark as fast as possible, but rather understand what went wrong here so it won't impact my real benchmarking.
Adam Morrison
A: 

It's the hotspot compiler at work. AFAIK, the first time it runs the function, runs "interpreted" and the execution path is analyzed, then the JIT compiler can optimize the subsequent function calls.

GClaramunt
It should replace the code on the stack midway through (that was introduced in 2000). Perhaps there is something in this example that is preventing it.
Tom Hawtin - tackline
+2  A: 

Add class loading in as a suspect. Classes are loaded lazily on first reference. So the first time the code runs, you're probably referencing some classes for the first time.

John M
+1  A: 

I suggest you make nThread = Runtime.getRuntime().availableProcessors() This will give you the optimal number of threads to use all the cores in your system.

You can try turning off the JIT to see what difference it makes.

Peter Lawrey
Thanks, I'm not trying to make this specific code fast, but rather trying to understand what is going on behind this scene to make it exhibit this behavior.
Adam Morrison
When Java first runs code it does so in an interperated mode. It does this as most code is only executed once and its not worth coding code in this situations. When code is used repeatedly it is optimised and can get further optimised based on changes in how the code is used. By default, full optimisation waits until something is called 10,000 times. It does this to collect statistics on how the code is used before optimises the code. e.g. Reducing this number can lead to poorer performance in the long run.
Peter Lawrey
A: 

It's most certainly the hotspot compiler. If you're running on 64 bit solaris it defaults to the server VM and hotspot just start optimizing on first execution. On the client VM the code may need to run a few times before hotspot kicks in. (i believe solaris only has the server vm but I may be wrong)

krosenvold
+1  A: 

You can get the VM to log information about classloading and compilation, try the following VM args: -XX:+PrintCompilation -XX:+TraceClassLoading This might give some further clues as to what's happening under the hood.

EDIT: I'm not sure those options work in java 1.5 (I've used them in 1.6). I'll try to check... EDIT again: It works in java 1.5 (note you need +, not -, or you turn the option off...)

bm212
A: 

See http://java.sun.com/javase/6/docs/technotes/guides/vm/server-class.html for how the launcher selects between client and server VM, and what is supported on the different processors and OSes.

stili
+2  A: 

The best way to verify if the JIT compiler is the reason for the speedup in later iterations is to run the benchmark with the JIT compiler turned off. To do this, specify the system property java.compiler=NONE (the word "none" must be in upper case).

Time spent doing class loading can also cause the benchmarked code to run slower the first time. Finally, there is a nondeterministic delay between calling Thread.start() and the Thread's run() method being called.

You might want to consider finding a benchmark framework. A good framework will "warm up" the code by running several iterations, then do multiple timings with a different number of iterations. See Java theory and practice: Anatomy of a flawed microbenchmark.

NamshubWriter
+1  A: 

I believe you can also use the non-standard option for the java command of -Xint to disable HotSpot and have your code interpreted only. This could at least take HotSpot out of the equation for interpreting your timing.

monceaux
I tried that, but the code ran for so long I gave up.
Adam Morrison
+3  A: 

Some ugly, unrealistic code (the stuff of microbenchmarks):

                while (counter < 10000000) {
                        // work
                        for (int j = 0; j < 100; j++)
                                counter++;
                        counter -= 99;
                }

So what is this doing and how fast should it run.

The inner loop increments counter 100 times, then the counter is decremented by 99. So an increment of 1. Note counter is a member variable of an outer class, so some overhead there. This is then run 10,000,000 times. So the inner loop is run 1,000,000,000 times.

A loop using to accessor methods, call it 25 cycles. 1,000,000,000 times at 1 GHz, gives 25s.

Hey, we predicted the SLOW time. The slow time is fast. The fast times are after the benchmark has been broken in some way - 2.5 cycles an iteration? Use -server and you might find it gets even more silly.

Tom Hawtin - tackline
accesor methods can be inlined, eliminating the overhead. loops can be unrooled, reducing the overhead and modern processors have branch prediction and may be able to do several operations at once, so an average of 2,5 cycles per iteration is normal.
ggf31416
and loops can be removed - easier than inlining virtual methods. so an average of 0.0 cycles per iteration is normal.
Tom Hawtin - tackline
Tom, thanks. Following your hint I also benchmarked this code in C (verifying the assembly code) and it took on the order of 20 seconds as well. So I attached a debugger to the Java process. Turns out that after the first measure() call, the JIT compiler optimizes out the memory accesses in the inner loop and makes the loop work on registers.
Adam Morrison
I wouldn't be surprised even if JIT would eventually optimize that code into "count = 10000000;"
Esko Luontola
Yeah, it's the sort of thing server HotSpot might do. Might well do it if you assign counter to zero. Currently it would have to do something like if (this$0.count < 10000000) this$0.count = 10000000;. Not the best benchmark.
Tom Hawtin - tackline
+4  A: 
Esko Luontola
For 20 seconds??
Tom Hawtin - tackline
True, it would be unusual for that to take so long, unless the system was something seriously broken. Tom and Adam found out the real reason.
Esko Luontola