Integer.equals()和Objects.equals()的比较 [英] Comparasion of Integer.equals() and Objects.equals()

查看:95
本文介绍了Integer.equals()和Objects.equals()的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对两个equals方法的测试:

Here are my test for two equals methods:

    Random generator = new Random();
    long startTime = System.nanoTime();
    for(int i = 0; i<1000; i++) {
        Integer int11 = generator.nextInt(1000);
        Integer int22 = generator.nextInt(1000);
        int11.equals(int22);            
    }
    long endTime = System.nanoTime();
    long duration = (endTime - startTime); 
    System.out.println(duration + " ns");

    Random generator1 = new Random();
    long startTime1 = System.nanoTime();
    for(int i = 0; i<1000; i++) {
        Integer int1 = generator1.nextInt(1000);
        Integer int2 = generator1.nextInt(1000);
        Objects.equals(int1, int2);
    }
    long endTime1 = System.nanoTime();
    long duration1 = (endTime1 - startTime1); 
    System.out.println(duration1 + " ns");

我只是想知道,Objects.equals()方法要慢多少,但是我得到了以下输出:

I just wanted to know, how much slower is the Objects.equals() method, but I got following output:

1005750 ns
650554 ns

当我替换这两种方法时:

When I replaced these two methods:

    Random generator1 = new Random();
    long startTime1 = System.nanoTime();
    for(int i = 0; i<1000; i++) {
        Integer int1 = generator1.nextInt(1000);
        Integer int2 = generator1.nextInt(1000);
        Objects.equals(int1, int2);
    }
    long endTime1 = System.nanoTime();
    long duration1 = (endTime1 - startTime1); 
    System.out.println(duration1 + " ns");

    Random generator = new Random();
    long startTime = System.nanoTime();
    for(int i = 0; i<1000; i++) {
        Integer int11 = generator.nextInt(1000);
        Integer int22 = generator.nextInt(1000);
        int11.equals(int22);            
    }
    long endTime = System.nanoTime();
    long duration = (endTime - startTime); 
    System.out.println(duration + " ns");

我得到的输出与上一个非常相似:

I get very similar output to the previous one:

1026871 ns
614074 ns

因此,我的问题是:在两种情况下,为什么第二个测试"的执行速度都比第一个更快?它取决于什么?

So, my question is: Why does the second "test" execute much faster than the first one in both cases? What does it depend on?

推荐答案

由于JIT启动,并检测到 Random.nextInt() equals()通常会调用两种方法,因此对它们进行优化是很有用的.

Because the JIT kicks in, and detects that Random.nextInt() and equals() are two methods that are often called, and that optimizing them is thus useful.

一旦字节码被优化并转换为本地代码,其执行速度就会更快.

Once the byte-code is optimized and transformed to native code, its execution is faster.

请注意,您所测量的可能是equal.nextInt()而不是equals().

Note that what you're measuring is probably more Random.nextInt() than equals().

由于JIT对运行时进行了优化,并且由于进行了垃圾回收,因此在Java中很难正确地执行正确的微基准测试.如果您想认真对待,请使用JMH.

Correct micro-benchmarks are extremely hard to do right in Java, due to the runtime optimizations done by the JIT, and due to garbage collection. If you want to be serious about it, use JMH.

这篇关于Integer.equals()和Objects.equals()的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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