Why does my algorithm become faster after executing several times? (JAVA)

I have a Sudoku solution algorithm, and my goal is to do it as soon as possible To test the algorithm, I run it several times and calculate the average After noticing some strange numbers, I decided to print all the time and get this result:

Execution Time : 4.257746 ms (#1)
Execution Time : 7.610686 ms (#2)
Execution Time : 6.277609 ms (#3)
Execution Time : 7.595707 ms (#4)
Execution Time : 7.610131 ms (#5)
Execution Time : 5.011104 ms (#6)
Execution Time : 3.970937 ms (#7)
Execution Time : 3.923783 ms (#8)
Execution Time : 4.070238 ms (#9)
Execution Time : 4.765347 ms (#10)
Execution Time : 0.818264 ms (#11)
Execution Time : 0.620216 ms (#12)
Execution Time : 0.679021 ms (#13)
Execution Time : 0.643516 ms (#14)
Execution Time : 0.718408 ms (#15)
Execution Time : 0.744481 ms (#16)
Execution Time : 0.760569 ms (#17)
Execution Time : 0.80384 ms (#18)
Execution Time : 0.75946 ms (#19)
Execution Time : 0.802176 ms (#20)
Execution Time : 66.032508 ms : average = 3.3016254000000003

After 10 to 15 executions (random changes), the performance of the algorithm is greatly improved If I run hundreds of times, I will eventually stabilize at about 0.3 Ms Note that I run the algorithm once before this loop of the JIT to do its work

In addition, if I let the thread sleep for 2 seconds and then run my loop, all my time is 1ms (/ – 0.2)

In addition, if I solve a general Sudoku (a 1-9 diagonal grid) about 500 times before the loop, all my time is about 0.3ms (/ – 0.02)

Every solution is the same All values are reset

So my question is multiple:

– why is each solution time improved and solved continuously?

– why does the resolution time drop suddenly after 10-15?

Solution

This is because the JIT compiles the method after the JVM makes a certain number of frequent calls to the method In practice, methods are not compiled the first time they are called For each method, the JVM maintains a call count, which is incremented each time it is called The JVM interprets a method until its call count exceeds the JIT compilation threshold When the call count reaches the threshold, the JIT compiles and optimizes the bytecode to run faster the next time it is called by the JVM Therefore, in your case, the performance of the algorithm will be greatly improved after every 10 to 15 (random) executions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>