我有一个带有2个计数器的循环:i和j。如果它们具有相同的值-迭代比它们的值不同要快得多:
Benchmark Mode Cnt Score Error Units
FloatsArrayBenchmark.times thrpt 20 341805.800 ± 1623.320 ops/s
FloatsArrayBenchmark.times2 thrpt 20 198764.909 ± 1608.387 ops/s
Java字节码是相同的,这意味着它与一些较低级别的优化有关。有人可以解释为什么会这样吗?这是基准:import org.openjdk.jmh.annotations.*;
public class FloatsArrayBenchmark {
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(new String[]{FloatsArrayBenchmark.class.getSimpleName()});
}
@Benchmark @Fork(value = 1, warmups = 0)
public void times(Data data) {
float[] result = new float[10000];;
for (int i = 0, j=0; i < 9_999; i++,j++)
result[j] = data.floats[i] * 10;
}
@Benchmark @Fork(value = 1, warmups = 0)
public void times2(Data data) {
float[] result = new float[10000];
for (int i = 0,j=1; i < 9_999; i++,j++)
result[j] = data.floats[i] * 10;
}
@State(Scope.Benchmark)
public static class Data {
private final float[] floats = new float[10000];
}
}
环境:最佳答案
在第一个(更快)版本中,i
始终(有效)与j
具有相同的值,因此:
public void times(Data data) {
float[] result = new float[10000];;
for (int i=0, j=0; i < 9_999; i++,j++)
result[j] = data.floats[i] * 10;
}
可以不用j
重写而具有相同的效果:public void times(Data data) {
float[] result = new float[10000];;
for (int i = 0; i < 9_999; i++)
result[i] = data.floats[i] * 10;
}
编译器很可能认识到j
是多余的并已将其消除,导致执行的++
操作数量减少了一半,占所有算术操作的1/3。这与时间一致:第二个版本每次迭代花费70%的时间。 70%大约是50%,以3:2的比例进行操作时,预期会得到该结果。
关于java - 对于循环性能: counters with same value vs.不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63784415/