Java 本地 vs 实例变量访问速度 [英] Java local vs instance variable access speed

查看:26
本文介绍了Java 本地 vs 实例变量访问速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是关于 Java 中的可变访问速度.今天在我的CS"中(如果你可以这么称呼的话),老师展示了一个类似于以下列表的例子:

So my question is about variable accessing speed in Java. Today in my "CS" (if you can call it that) the teacher presented a similar example to the following of a List:

public class ListExample<T> {
    private Node<T> head;
    private Node<T> tail;

    private class Node<T> { /* ... */ }

    public void append(T content) {
        if (!isEmpty()) {
            Node<T> dummy = new Node<T>(content);
            head = dummy;
            tail = dummy;

            head.setNext(head);
            // or this
            dummy.setNext(dummy);

        } else { /* ... */ }
    }

    // more methods
    // ...
}

我的问题是:对 head.setNext(head) 的调用会比 dummy.setNext(dummy) 慢吗?即使它并不引人注目.我想知道这一点,因为 head 显然是并且类的实例 var 和 dummy 是本地的,那么本地访问会更快吗?

My question is: Would the call to head.setNext(head) be slower than dummy.setNext(dummy) ? Even if it's not noticeable. I was wondering this since head is obviously and instance var of the class and dummy is local, so would the local access be faster?

推荐答案

好的,我已经写了一个微基准测试(正如@Joni & @MattBall 所建议的那样),这里是每个 1 x 1000000000 次访问的结果一个局部变量和一个实例变量:

Ok, I've written a micro-benchmark (as suggested by @Joni & @MattBall) and here are the results for 1 x 1000000000 accesses for each a local and an instance variable:

Average time for instance variable access: 5.08E-4
Average time for local variable access: 4.96E-4

对于 10 x 1000000000 次访问:

For 10 x 1000000000 accesses each:

Average time for instance variable access:4.723E-4
Average time for local variable access:4.631E-4

对于 100 x 1000000000 次访问:

For 100 x 1000000000 accesses each:

Average time for instance variable access: 5.050300000000002E-4
Average time for local variable access: 5.002400000000001E-4

所以看起来局部变量访问确实比实例变量访问更快(即使两者都指向同一个对象).

So it seems that local variable accesses are indeed faster that instance var accesses (even if both point to the same object).

注意:我不想发现这个,因为我想优化一些东西,这只是纯粹的兴趣.

Note: I didn't want to find this out, because of something I wanted to optimize, it was just pure interest.

附言这是微基准测试的代码:

P.S. Here is the code for the micro-benchmark:

public class AccessBenchmark {
    private final long N = 1000000000;
    private static final int M = 1;

    private LocalClass instanceVar;

    private class LocalClass {
        public void someFunc() {}
    }

    public double testInstanceVar() {
        // System.out.println("Running instance variable benchmark:");
        instanceVar = new LocalClass();

        long start = System.currentTimeMillis();
        for (int i = 0; i < N; i++) {
            instanceVar.someFunc();
        }

        long elapsed = System.currentTimeMillis() - start;

        double avg = (elapsed * 1000.0) / N;

        // System.out.println("elapsed time = " + elapsed + "ms");
        // System.out.println(avg + " microseconds per execution");

        return avg;
    }

    public double testLocalVar() {
        // System.out.println("Running local variable benchmark:");
        instanceVar = new LocalClass();
        LocalClass localVar = instanceVar;

        long start = System.currentTimeMillis();
        for (int i = 0 ; i < N; i++) {
            localVar.someFunc();
        }

        long elapsed = System.currentTimeMillis() - start;

        double avg = (elapsed * 1000.0) / N;

        // System.out.println("elapsed time = " + elapsed + "ms");
        // System.out.println(avg + " microseconds per execution");

        return avg;
    }

    public static void main(String[] args) {
        AccessBenchmark bench;

        double[] avgInstance = new double[M];
        double[] avgLocal = new double[M];

        for (int i = 0; i < M; i++) {
            bench = new AccessBenchmark();

            avgInstance[i] = bench.testInstanceVar();
            avgLocal[i] = bench.testLocalVar();

            System.gc();
        }

        double sumInstance = 0.0;
        for (double d : avgInstance) sumInstance += d;
        System.out.println("Average time for instance variable access: " + sumInstance / M);

        double sumLocal = 0.0;
        for (double d : avgLocal) sumLocal += d;
        System.out.println("Average time for local variable access: " + sumLocal / M);
    }
}

这篇关于Java 本地 vs 实例变量访问速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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