减少 Java 程序相同连续运行的执行时间

编程入门 行业动态 更新时间:2024-10-15 06:15:12
本文介绍了减少 Java 程序相同连续运行的执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

背景:我的团队正在开发一个 Java 程序,该程序接受文本作为输入、处理它并显示结果.程序的每次运行都是无状态的并且独立于其他运行.我们注意到,在对同一个文本循环运行处理方法时,然后一致地,第一次运行需要最长的时间才能完成,接下来的运行时间越来越少,直到运行几十次后执行时间趋于稳定远低于初始运行时间的值.

Background: My team works on a Java program which accepts a text as input, processes it and displays the result. Each run of the program is stateless and independent of other runs. We noticed that when running the processing method in a loop on the same text, then consistently, the first run takes the longest time to complete, and the next runs take less and less time, until the execution time stabilizes after a few dozen runs on a value much lower than the initial running time.

我尝试测试这是否是任何程序的普遍现象,并尝试运行以下具有嵌套循环的代码,并测量内循环花费的时间:

I tried to test if this is a general phenomenon for any program, and tried running the following code, which has a nested loop, and measured how long the inner loop took:

String s = "abcefghijklmnopqrstuvwxyz"; int TIMES = 10; int INNER_TIMES = 1000000; long count = 0; for (int i = 0; i < TIMES; i++) { long start = System.currentTimeMillis(); for (int j = 0; j < INNER_TIMES; j++) { List<String> list = new ArrayList<>(); list.add(s); } count++; long end = System.currentTimeMillis(); double time = (end - start) / 1000.0; System.out.println(count + ": " + time + " seconds"); }

结果与我们在程序中注意到的相似:

The results were similar to what we had noticed in our program:

1: 0.036 seconds 2: 0.018 seconds 3: 0.016 seconds 4: 0.009 seconds 5: 0.01 seconds 6: 0.009 seconds 7: 0.02 seconds 8: 0.014 seconds 9: 0.009 seconds 10: 0.01 seconds

我试过多次运行,结果非常一致.内循环的第一次运行总是需要大约 0.035-0.036 秒,从第 4 次运行开始(即使将 TIMES 增加到 1000)也需要大约 0.008-0.01 秒(有一些例外).

I tried running this many times, and the results were pretty much consistent. The first run of the inner loop always takes around 0.035-0.036 seconds, and from the 4th run and onwards (even when increasing TIMES to 1000) it takes around 0.008-0.01 seconds (with some exceptions).

我和我的团队负责人都对发生这种情况的原因感到困惑.这是 Java 中的已知现象吗?在一般的软件中?为什么看起来软件在达到最佳性能之前需要预热"?

Both my team leader and I were stumped about why this happens. Is this a known phenomenon in Java? In software in general? Why does it seem like the software needs to "warm up" before it reaches optimal performance?

推荐答案

这是 Java 应用程序的正常行为,这也意味着您不应该优化您的应用程序除非它被证明有瓶颈通过使用分析器.JVM 将通过使用 JIT(Just-In-Time) 在运行时编译代码.

That's normal behavior for Java applications, which also means that you should not optimize your applications unless it is proven to have a bottleneck identified by usage of a profiler. The JVM will improve the performance of the code by using JIT (Just-In-Time) compilation for your code at runtime.

您可以在此处找到有关 JIT 优化的更多信息:Java 热点性能引擎架构.第 3 章 Java HotSpot 编译器

You can find more info about JIT optimizations here: The Java HotSpot Performance Engine Architecture. Chapter 3. The Java HotSpot Compilers

除此之外,您正在对自己的代码进行非常幼稚的基准测试.我建议您阅读如何在 Java 中编写正确的微基准测试? 以增强当前的评估结果你的代码.我还建议使用 JMH,一个专用于 Java 平台的微型基准测试框架.

Besides this, you're doing a pretty naive benchmark of your own code. I suggest you to read How do I write a correct micro-benchmark in Java? to enhance the current results of the evaluation of your code. I also recommend to use JMH, a micro benchmark framework specific for Java platform.

更多推荐

减少 Java 程序相同连续运行的执行时间

本文发布于:2023-11-30 16:18:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:执行时间   程序   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!