Null/Object and Null/Null comparison efficiency

前端 未结 1 1849
别跟我提以往
别跟我提以往 2021-02-15 16:13

This question lead me to do some testing:

public class Stack
{
    public static void main(String[] args)
    {
        Object obj0 = null;
        Object obj1 =         


        
1条回答
  •  青春惊慌失措
    2021-02-15 16:43

    If you move last two loops to the beginning you will get the same results, so comparisons are irrelevant.

    It's all about JIT compiler warm-up. During the first 2 loops java starts with interpreting bytecode. After some iterations, it determines that code path is "hot", so it compiles it to machine code and removes the loops that have no effect, so you are basically measuring System.nanotime and double arithmetic.

    I'm not really sure why two loops are slow. I think that after it finds two hot paths it decides to optimize entire method.

    0 讨论(0)
提交回复
热议问题