本文介绍了内在函数和内联对Lambda性能的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了这个基准并在运行时我做了以下几点:

I have used this benchmark java8-lambda-performance-test and when running it I have done the followings:

1.Disabled Intrinsic usage

1.Disabled Intrinsic usage

2.Disabled Inlining

2.Disabled Inlining

3.Disabled编译
模式

3.Disabled Compiling mode

我发现禁用两个第一次优化对结果没有影响。

I have found out that disabling the two first optimizations has no effect on results.

这很奇怪,而且当使用和打印内在运行基准测试时,我没有找到任何对内部编译的LamboForm

Which is strange, and also when running the benchmark with and print intrinsic, I did not find any call to the intrinsic compiledLambdaForm

由于数学内在函数大量使用_min,_pow ...我期望禁用内在函数会降低性能

Since maths intrinsics are heavily used there _min,_pow... I was expecting that disabling intrinsics would have slown the performance

推荐答案

您没有注意到预期的性能影响的原因是。

我用,事情终于正确。

The reason why you haven't noticed an expected performance effect is poorly written benchmark.
I rewrote the benchmark using JMH and the things finally got right.

package lambdademo;

import org.openjdk.jmh.annotations.*;

import java.util.List;

@State(Scope.Benchmark)
public class LambdaBenchmark {
    @Param("100")
    private static int loopCount;

    private static double identity(double val) {
        double result = 0;
        for (int i=0; i < loopCount; i++) {
            result += Math.sqrt(Math.abs(Math.pow(val, 2)));
        }
        return result / loopCount;
    }

    private List<EmployeeRec> employeeList = new EmployeeFile().loadEmployeeList();

    @Benchmark
    public double streamAverage() {
        return streamAverageNoInline();
    }

    @Benchmark
    @Fork(jvmArgs = "-XX:-Inline")
    public double streamAverageNoInline() {
        return employeeList.stream()
                .filter(s -> s.getGender().equals("M"))
                .mapToDouble(s -> s.getAge())
                .average()
                .getAsDouble();
    }

    @Benchmark
    public double streamMath() {
        return streamMathNoIntrinsic();
    }

    @Benchmark
    @Fork(jvmArgs = {"-XX:+UnlockDiagnosticVMOptions", "-XX:DisableIntrinsic=_dpow,_dabs,_dsqrt"})
    public double streamMathNoIntrinsic() {
        return employeeList.stream()
                .filter(s -> s.getGender().equals("M"))
                .mapToDouble(s -> identity(s.getAge()))
                .average()
                .getAsDouble();
    }
}

以下是结果:

Benchmark                              Mode  Cnt     Score    Error  Units
LambdaBenchmark.streamAverage          avgt    5    71,490 ±  0,770  ms/op
LambdaBenchmark.streamAverageNoInline  avgt    5   122,740 ±  0,576  ms/op
LambdaBenchmark.streamMath             avgt    5    92,672 ±  1,538  ms/op
LambdaBenchmark.streamMathNoIntrinsic  avgt    5  5747,007 ± 20,387  ms/op

正如预期的那样,基准 -XX:-Inline 的工作时间延长了70%,带有数学内在函数的版本禁用似乎慢了60倍!

As expected, the benchmark with -XX:-Inline works 70% longer, and the version with Math intrinsics disabled appears to be 60 times slower!

这篇关于内在函数和内联对Lambda性能的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:18