Null / Object和Null / Null比较效率 [英] Null/Object and Null/Null comparison efficiency

查看:131
本文介绍了Null / Object和Null / Null比较效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,有趣的是,当对象时,比较的速度有多快我们正在比较的是初始化。每个输出中的前两个数字是 Object null 时的后两个数字,后两个数字是<$ c时$ c>对象已初始化。我执行了21次额外的程序执行,在所有30次执行中,当初始化 Object 时,比较速度要快得多。这里发生了什么?

解决方案

如果将最后两个循环移到开头,您将得到相同的结果,因此比较无关紧要。



这是关于JIT编译器预热的全部内容。在前2个循环中, java 以解释字节码开始。在一些迭代之后,它确定代码路径是热,因此它将其编译为机器代码并删除无效的循环,因此您基本上测量 System.nanotime double 算术。



我不确定为什么两个循环都很慢。我认为在找到两条热门路径后,它决定优化整个方法。


This question lead me to do some testing:

public class Stack
{
    public static void main(String[] args)
    {
        Object obj0 = null;
        Object obj1 = new Object();
        long start;
        long end;
        double difference;  
        double differenceAvg = 0;

        for (int j = 0; j < 100; j++)
        {
            start = System.nanoTime();

            for (int i = 0; i < 1000000000; i++)
                if (obj0 == null);

            end = System.nanoTime();
            difference = end - start;
            differenceAvg +=difference;
        }

        System.out.println(differenceAvg/100);
        differenceAvg = 0;

        for (int j = 0; j < 100; j++)
        {
            start = System.nanoTime();

            for (int i = 0; i < 1000000000; i++)
                if (null == obj0);

            end = System.nanoTime();
            difference = end - start;
            differenceAvg +=difference;
        }

        System.out.println(differenceAvg/100);
        differenceAvg = 0;

        for (int j = 0; j < 100; j++)
        {
            start = System.nanoTime();

            for (int i = 0; i < 1000000000; i++)
                if (obj1 == null);

            end = System.nanoTime();
            difference = end - start;
            differenceAvg +=difference;
        }

        System.out.println(differenceAvg/100);
        differenceAvg = 0;

        for (int j = 0; j < 100; j++)
        {
            start = System.nanoTime();

            for (int i = 0; i < 1000000000; i++)
                if (null == obj1);

            end = System.nanoTime();
            difference = end - start;
            differenceAvg +=difference;
        }

        System.out.println(differenceAvg/100);      
    }
}

Tangential to the other post, it's interesting to note how much faster the comparison is when the Object that we're comparing is initialized. The first two numbers in each output are when the Object was null and the latter two numbers are when the Object was initialized. I ran 21 additional executions of the program, in all 30 executions, the comparison was much faster when the Object was initialized. What's going on here?

解决方案

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.

这篇关于Null / Object和Null / Null比较效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆