Java - 重复的函数调用减少了执行时间 [英] Java - Repeated function call reduces execution time

查看:145
本文介绍了Java - 重复的函数调用减少了执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

public class BenchMark {
    public static void main(String args[]) {
        doLinear();

        doLinear();

        doLinear();

        doLinear();

    }


    private static void doParallel() {
        IntStream range = IntStream.range(1, 6).parallel();

        long startTime = System.nanoTime();
        int reduce = range
                .reduce((a, item) -> a * item).getAsInt();
        long endTime = System.nanoTime();
        System.out.println("parallel: " +reduce + " -- Time: " + (endTime - startTime));
    }

    private static void doLinear() {
        IntStream range = IntStream.range(1, 6);

        long startTime = System.nanoTime();
        int reduce = range
                .reduce((a, item) -> a * item).getAsInt();
        long endTime = System.nanoTime();
        System.out.println("linear: " +reduce + " -- Time: " + (endTime - startTime));
    }

}

我试图对流进行基准测试但是在一次又一次地调用相同功能时,执行时间逐渐减少

I was trying to benchmark streams but came through this execution time steadily decreasing upon calling the same function again and again

输出:

linear: 120 -- Time: 57008226
linear: 120 -- Time: 23202
linear: 120 -- Time: 17192
linear: 120 -- Time: 17802

Process finished with exit code 0

第一次和第二次执行时间之间有一个巨大差异

There is a huge difference between first and second execution time.

我确信JVM可能会在幕后做一些技巧但可以任何人帮我理解那里真的发生了什么?

I'm sure JVM might be doing some tricks behind the scenes but can anybody help me understand whats really going on there ?

无论如何都要避免这种优化,以便我可以确定真正的执行时间吗?

Is there anyway to avoid this optimization so I can benchmark true execution time ?

推荐答案


我确信JVM可能会在幕后做一些技巧,但任何人都可以帮我理解那里真正发生的事情吗?

I'm sure JVM might be doing some tricks behind the scenes but can anybody help me understand whats really going on there?




  1. 第一次调用的大量延迟是由于完整的lambda运行时子系统的初始化。您只需为整个应用程序支付一次。

  1. The massive latency of the first invocation is due to the initialization of the complete lambda runtime subsystem. You pay this only once for the whole application.

您的代码第一次到达任何给定的lambda表达式时,您需要支付链接那个lambda(初始化 invokedynamic 调用网站)。

The first time your code reaches any given lambda expression, you pay for the linkage of that lambda (initialization of the invokedynamic call site).

经过一些迭代后你会由于JIT编译器优化了缩减代码,请参阅额外的加速。

After some iterations you'll see additional speedup due to the JIT compiler optimizing your reduction code.




无论如何为了避免这种优化,我可以确定真正的执行时间吗?

Is there anyway to avoid this optimization so I can benchmark true execution time?

你在这里要求一个矛盾:真正的执行时间是预热后获得的那个,当所有优化都已应用时。这是实际应用程序将遇到的运行时。前几次运行的延迟与更广泛的图片无关,除非您对单次拍摄表现感兴趣。

You are asking for a contradiction here: the "true" execution time is the one you get after warmup, when all optimizations have been applied. This is the runtime an actual application would experience. The latency of the first few runs is not relevant to the wider picture, unless you are interested in single-shot performance.

为了探索,您可以看到禁用JIT编译时代码行为:将 -Xint 传递给 java 命令。还有更多的标志禁用了优化的各个方面。

For the sake of exploration you can see how your code behaves with JIT compilation disabled: pass -Xint to the java command. There are many more flags which disable various aspects of optimization.

这篇关于Java - 重复的函数调用减少了执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆