java - Java 中循环的性能

标签 java performance loops bit-shift

我刚刚用 Java 中的循环做了一个小测试。我假设 Java 中的位移位速度通常比默认的整数递增更快。所以这是我的示例代码:

final int n = 16;
long n1 = System.nanoTime();
for (int i = 1; i < 1 << n; i <<= 1) {
    // nothing
}
long n2 = System.nanoTime();
for (int i = 0; i < n; i++) {
    // nothing
}
long n3 = System.nanoTime();
System.out.println("with shift = " + (n2 - n1) + " ns");
System.out.println("without shift = " + (n3 - n2) + " ns");

所以我的想法是,n1 和 n2 之间的时间会小于 n2 和 n3 之间的时间。 但是每次我运行这段代码时,整数递增似乎都更快了。 这是上面代码的输出:

with shift = 2445 ns
without shift = 1885 ns

with shift = 2374 ns
without shift = 1886 ns

with shift = 2374 ns
without shift = 1607 ns

有人可以解释一下这种行为吗?答案是 JVM 编译这段代码的方式还是基于底层架构?

Ubuntu Linux 3.5.0-17-generic i686 GNU/Linux
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 23
model name  : Pentium(R) Dual-Core CPU       T4300  @ 2.10GHz
stepping    : 10
microcode   : 0xa07
cpu MHz     : 1200.000
cache size  : 1024 KB
physical id : 0
siblings    : 2
core id     : 0
cpu cores   : 2
apicid      : 0
initial apicid  : 0
fdiv_bug    : no
hlt_bug     : no
f00f_bug    : no
coma_bug    : no
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm dtherm
bogomips    : 4189.42
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:

processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 23
model name  : Pentium(R) Dual-Core CPU       T4300  @ 2.10GHz
stepping    : 10
microcode   : 0xa07
cpu MHz     : 1200.000
cache size  : 1024 KB
physical id : 0
siblings    : 2
core id     : 1
cpu cores   : 2
apicid      : 1
initial apicid  : 1
fdiv_bug    : no
hlt_bug     : no
f00f_bug    : no
coma_bug    : no
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm dtherm
bogomips    : 4189.42
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:

========== 编辑 ===============

好的,所以我更新了我的代码以获得更好的测量结果。

我的 JVM:

java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06)
Java HotSpot(TM) Server VM (build 20.12-b01, mixed mode)

新代码:

// amount of shifts
final int n = 16;
// recorded times
long n1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0;
// measured times
long withShiftFor = Long.MAX_VALUE;
long withoutShiftFor = Long.MAX_VALUE;
long withShiftWhile = Long.MAX_VALUE;
long withoutShiftWhile = Long.MAX_VALUE;
// instance to operate with
boolean b = true;
// do some loops to measure a better result
for (int x = 0; x < 2000000; x++) {
    // for loop with shift
    n1 = System.nanoTime();
    for (int i = 1; i < 1 << n; i <<= 1) {
        b = !b;
    }
    // for loop wihtout shift
    n2 = System.nanoTime();
    for (int i = 0; i < n; i++) {
        b = !b;
    }
    // while loop with shift
    n3 = System.nanoTime();
    int i = 1;
    while (i < 1 << n) {
        b = !b;
        i <<= 1;
    }
    // while loop without shift
    n4 = System.nanoTime();
    int j = 0;
    while (j < n) {
        b = !b;
        j++;
    }
    n5 = System.nanoTime();
    // take minimal time to save best result
    withShiftFor = Math.min(withShiftFor, n2 - n1);
    withoutShiftFor = Math.min(withoutShiftFor, n3 - n2);
    withShiftWhile = Math.min(withShiftWhile, n4 - n3);
    withoutShiftWhile = Math.min(withoutShiftWhile, n5 - n4);
}
System.out.println("for with shift = " + withShiftFor + " ns");
System.out.println("for without shift = " + withoutShiftFor + " ns");
System.out.println("while with shift = " + withShiftWhile + " ns");
System.out.println("while without shift = " + withoutShiftWhile + " ns");

3 次运行后的新输出(每次运行超过 5 秒):

for with shift = 907 ns
for without shift = 838 ns
while with shift = 907 ns
while without shift = 907 ns

for with shift = 907 ns
for without shift = 907 ns
while with shift = 907 ns
while without shift = 907 ns

for with shift = 907 ns
for without shift = 838 ns
while with shift = 907 ns
while without shift = 907 ns

所以你是对的,经过几秒钟和大量迭代后,结果几乎相同。但是为什么没有移动的for循环比其他解决方案更快? jvm 是否有任何优化,尽管一行用于增量而不是 4 行通过移位你提到的?为什么递增的 while 和其他循环一样快?

最佳答案

Can someone please explain this beahviour? Is the answer in the way how the JVM compiles this code or is it based on the underlying architecture?

当您运行短循环时,代码会被解释。因此,如果您不打算经常运行代码或无法预热代码,那么您应该对其进行基准测试,并期待像您所拥有的那样的奇怪结果。

如果你想比较编译/优化的代码,你应该忽略前 10K 到 20K 循环,因为一个循环需要迭代 10K 时间来默认编译(然后在后台编译它需要一点时间)

无论如何,我还建议运行测试至少 2 秒以减少变化。

你的循环不做任何事情,我希望 JIT 能够消除它们,你最终只是计算执行 System.nanoTime() 所需的时间,这可能会增加 40 - 1000 ns,具体取决于系统。

关于java - Java 中循环的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13624479/

相关文章:

java - 迭代存储在字符串中的一系列位

python - 使用Python迭代列中的元素以进行预处理

python - 如何使内循环考虑外循环的每次迭代?

c# - 使用静态变量来存储全局的、不断变化的信息是一种好习惯吗?

performance - lerna 构建非常慢,总是(+30 分钟)

java - Android中的SQLite如何更新特定行

c - 返回 64 位整数中所有设置位的位置的最快方法是什么?

即使全新安装,Magento 管理后端也非常慢

java - 删除按钮之间的空间

java - 文本字段的 NulPointerException