Java本地vs实例变量访问速度
发布时间:2020-12-15 07:38:09 所属栏目:Java 来源:网络整理
导读:所以我的问题是关于 Java中的变量访问速度.今天在我的“CS”(如果你可以称之为)中,老师提出了类似下面的列表示例: public class ListExampleT { private NodeT head; private NodeT tail; private class NodeT { /* ... */ } public void append(T content)
|
所以我的问题是关于
Java中的变量访问速度.今天在我的“CS”(如果你可以称之为)中,老师提出了类似下面的列表示例:
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)慢?即使它不明显. 解决方法
好的,我已经编写了一个微基准测试程序(由@Joni& @MattBall建议),以下是每个本地变量和一个实例变量的1 x 1000000000次访问的结果:
Average time for instance variable access: 5.08E-4 Average time for local variable access: 4.96E-4 每次10 x 1000000000次访问: Average time for instance variable access:4.723E-4 Average time for local variable access:4.631E-4 对于100 x 1000000000访问每个: Average time for instance variable access: 5.050300000000002E-4 Average time for local variable access: 5.002400000000001E-4 所以看起来局部变量访问确实比实例var访问更快(即使两者都指向同一个对象). 注意:我不想找到这个,因为我想要优化的东西,这只是纯粹的兴趣. 附:以下是微基准的代码: 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);
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
