Performance Explanation: code runs slower after warm up

后端 未结 2 1384
误落风尘
误落风尘 2021-02-01 20:44

The code below runs the exact same calculation 3 times (it does not do much: basically adding all numbers from 1 to 100m). The first 2 blocks run approximately 10 times faster t

2条回答
  •  野的像风
    2021-02-01 21:06

    You need to place each loop in a different method. The reason you need to do this is that the JIT collections statistics on how the code was run and this is used to optimise the code. However, a method is optimised after it is called 10000 time or a loop is run 10000 times.

    In your case, the first loop trigger the whole method to be optimised, even though the later loops have not been run so no statistics have been collected. Place each loop in its own method and this won't happen.

提交回复
热议问题