简体   繁体   中英

Why JVM executes same program faster over time when launched?

I have written a simple SUDOKU solver. To roughly test the performance I'm using simple System.currentTimeMillis calls.

I have prepared a set of initial sudoku configuration in text file. The program reads the file and solves each sudoku configuration. When running the tests I have noticed that the first 3-4 solve runs are really slower than the rest and by slower I mean by order of magnitude.

There is sample pseudo-code snippet:

main(){
     while(file has lines){
         configuration = readLine();
         Solver s = new Solver(configuration);

         now1 = System.currentTimeMillis();
         s.solve();
         now2 = System.currentTimeMillis();

         System.out.print(now2 - now1);
     }
}

I measure only solve() method, so IO is not a problem, I even hardcoded some data into program - still first few slower. The difficulty of puzzle is not an issue as well I have tried different permutations and difficulties of configuration and always the same - first few are slower.

My question is - why is that and is there a way to prevent it?

This is supposed to happen. The JIT compiler optimizes code that gets called more often as your program runs for longer.

This only reflects the general fact that the technique you're using to test performance simply isn't reliable in Java.

In Practice, Methods are not compiled by JIT the first time they are called by JVM . For each method, the JVM maintains a call count , which is incremented every time the method is called. The JVM interprets a method until its call count exceeds a JIT compilation threshold . Therefore, often-used methods are compiled soon after the JVM has started , and less-used methods are compiled much later, or not at all. This JIT compilation Threshold helps the JVM start quickly.

So, the busiest methods of java program are always optimized most aggresively which increases its execution speed each time it is called by program.

Here is the Source for above information.

In performance testing engagements we'd always run the system being tested for a while to let it reach a steady state. Only then would we start the performance metrics. You might try the same: run the solve() method a number of times before capturing your metrics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM