gpt4 book ai didi

java - 了解 jvm 中的循环性能

转载 作者:搜寻专家 更新时间:2023-10-30 21:05:45 26 4
gpt4 key购买 nike

我正在玩 jmh 并且在关于 looping 的部分他们说

You might notice the larger the repetitions count, the lower the "perceived" cost of the operation being measured. Up to the point we do each addition with 1/20 ns, well beyond what hardware can actually do. This happens because the loop is heavily unrolled/pipelined, and the operation to be measured is hoisted from the loop. Morale: don't overuse loops, rely on JMH to get the measurement right.

我自己试过

    @Benchmark
@OperationsPerInvocation(1)
public int measurewrong_1() {
return reps(1);
}

@Benchmark
@OperationsPerInvocation(1000)
public int measurewrong_1000() {
return reps(1000);
}

得到如下结果:

Benchmark                      Mode  Cnt  Score    Error  Units
MyBenchmark.measurewrong_1 avgt 15 2.425 ± 0.137 ns/op
MyBenchmark.measurewrong_1000 avgt 15 0.036 ± 0.001 ns/op

它确实表明 MyBenchmark.measurewrong_1000MyBenchmark.measurewrong_1 快得多。但我无法真正理解 JVM 为提高性能所做的优化。

循环展开/流水线化是什么意思?

最佳答案

循环展开使流水线成为可能。因此,流水线 CPU(例如 RISC)可以并行执行展开的代码。

因此,如果您的 CPU 能够并行执行 5 个流水线,您的循环将按以下方式展开:

// pseudo code
int pipelines = 5;
for(int i = 0; i < length; i += pipelines){
s += (x + y);
s += (x + y);
s += (x + y);
s += (x + y);
s += (x + y);
}

Risc pipeline

IF = 指令获取,ID = 指令解码,EX = 执行,MEM = 内存访问,WB = 寄存器写回

来自 Oracle White paper :

... a standard compiler optimization that enables faster loop execution. Loop unrolling increases the loop body size while simultaneously decreasing the number of iterations. Loop unrolling also increases the effectiveness of other optimizations.

关于流水线的更多信息:Classic RISC pipeline

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com