为什么方法访问似乎比字段访问快? [英] Why does Method access seem faster than Field access?

查看:80
本文介绍了为什么方法访问似乎比字段访问快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些测试,以了解使用getter/setter和直接现场访问之间的速度差异.我写了一个像这样的简单基准测试应用程序:

I was doing some tests to find out what the speed differences are between using getters/setters and direct field access. I wrote a simple benchmark application like this:

public class FieldTest {

    private int value = 0;

    public void setValue(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }

    public static void doTest(int num) {
        FieldTest f = new FieldTest();

        // test direct field access
        long start1 = System.nanoTime();

        for (int i = 0; i < num; i++) {
            f.value = f.value + 1;
        }
        f.value = 0;

        long diff1 = System.nanoTime() - start1;

        // test method field access
        long start2 = System.nanoTime();

        for (int i = 0; i < num; i++) {
            f.setValue(f.getValue() + 1);
        }
        f.setValue(0);

        long diff2 = System.nanoTime() - start2;

        // print results
        System.out.printf("Field Access:  %d ns\n", diff1);
        System.out.printf("Method Access: %d ns\n", diff2);
        System.out.println();
    }

    public static void main(String[] args) throws InterruptedException {
        int num = 2147483647;

        // wait for the VM to warm up
        Thread.sleep(1000);

        for (int i = 0; i < 10; i++) {
            doTest(num);
        }
    }

}

无论何时运行它,我都会得到如下一致的结果: http://pastebin.com/hcAtjVCL

Whenever I run it, I get consistent results such as these: http://pastebin.com/hcAtjVCL

我想知道是否有人可以向我解释为什么字段访问似乎比getter/setter方法访问慢,以及为什么最后8次迭代执行得如此快.

I was wondering if someone could explain to me why field access seems to be slower than getter/setter method access, and also why the last 8 iterations execute incredibly fast.

编辑:考虑到assyliasStephen C注释后,我将代码更改为 http://pastebin.com /wxiDdRix .

Edit: Having taken into account assylias and Stephen C comments, I have changed the code to http://pastebin.com/Vzb8hGdc where I got slightly different results: http://pastebin.com/wxiDdRix .

推荐答案

原因是您的基准测试已被破坏.

The explanation is that your benchmark is broken.

第一次迭代是使用解释器完成的.

The first iteration is done using the interpreter.

Field Access:  1528500478 ns
Method Access: 1521365905 ns

第二个迭代由解释器开始,然后转到运行JIT编译的代码.

The second iteration is done by the interpreter to start with and then we flip to running JIT compiled code.

Field Access:  1550385619 ns
Method Access: 47761359 ns

其余所有迭代均使用JIT编译代码完成.

The remaining iterations are all done using JIT compiled code.

Field Access:  68 ns
Method Access: 33 ns

etcetera

之所以如此之快 是因为JIT编译器已经优化了循环.它检测到它们没有为计算提供任何有用的信息. (尚不清楚为什么第一个数字似乎始终比第二个数字快,但我怀疑优化后的代码是否以任何有意义的方式来衡量字段与方法的访问.)

The reason they are unbelievably fast is that the JIT compiler has optimized the loops away. It has detected that they were not contributing anything useful to the computation. (It is not clear why the first number seems consistently faster than the second, but I doubt that the optimized code is measuring field versus method access in any meaningful way.)

重新获得 UPDATED 代码/结果:很明显,JIT编译器仍在优化循环.

Re the UPDATED code / results: it is obvious that the JIT compiler is still optimizing the loops away.

这篇关于为什么方法访问似乎比字段访问快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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