java - Java 11 字符串连接性能与 Java 8
问题描述
有谁知道为什么我在 Java 8 和 Java 11 上运行此代码时会获得如此不同的性能?
在不使用任何运行时标志的情况下,与 Java 8 相比,此代码在 Java 11 下的运行速度似乎要慢得多。
import java.util.Date;
public class PerformanceExperiment {
public static volatile String s = "";
public static void main(String[] args)
{
System.out.println("Starting performance test");
String s1 = "STRING ONE";
String s2 = "STRING TWO";
long now1 = (new Date()).getTime();
for (long i = 0; i < 1_000_000_00; i++)
{
s = "abc " + s1 + " def " + s2;
}
long now2 = (new Date()).getTime();
System.out.println("initial block took " + (now2 - now1) + "ms");
for (long i = 0; i < 4_000_000_00; i++)
{
s = "abc " + s1 + " def " + s2;
}
long now3 = (new Date()).getTime();
System.out.println("Main block took " + (now3 - now2) + "ms");
}
}
我尝试了许多命令行标志,但没有找到任何与 Java 8 性能相匹配的东西。
我只在 Windows 上对此进行了测试,因此它在其他操作系统上的行为可能会有所不同。
解决方案
我将您的应用修改为
- 使用
System.nanoTime()
代替以new Date()
获得更高的精度(有关更多信息,请参阅此答案:https ://stackoverflow.com/a/1776053/963076 )。 - 使用 Netbeans 分析器。
- 循环 10 次迭代
将 Netbeans 8.2 与 JDK 8 v181 一起使用:
Starting performance test 0
initial block took 3147ms
Main block took 9469ms
Starting performance test 1
initial block took 2398ms
Main block took 9601ms
Starting performance test 2
initial block took 2463ms
Main block took 9671ms
Starting performance test 3
initial block took 2464ms
Main block took 9565ms
Starting performance test 4
initial block took 2410ms
Main block took 9672ms
Starting performance test 5
initial block took 2418ms
Main block took 9598ms
Starting performance test 6
initial block took 2384ms
Main block took 9733ms
Starting performance test 7
initial block took 2402ms
Main block took 9610ms
Starting performance test 8
initial block took 2509ms
Main block took 11222ms
Starting performance test 9
initial block took 2455ms
Main block took 10661ms
探查器显示了这个遥测数据:
对于使用 JDK 11.0.2 的 Netbeans 10.0:
Starting performance test 0
initial block took 3760ms
Main block took 15056ms
Starting performance test 1
initial block took 3734ms
Main block took 14602ms
Starting performance test 2
initial block took 3615ms
Main block took 14762ms
Starting performance test 3
initial block took 3748ms
Main block took 14534ms
Starting performance test 4
initial block took 3628ms
Main block took 14759ms
Starting performance test 5
initial block took 3625ms
Main block took 14959ms
Starting performance test 6
initial block took 3987ms
Main block took 14967ms
Starting performance test 7
initial block took 3803ms
Main block took 14701ms
Starting performance test 8
initial block took 3599ms
Main block took 14762ms
Starting performance test 9
initial block took 3627ms
Main block took 14434ms
我的结论是:JDK 11 正在做更多的工作来提高内存效率。请注意,垃圾收集器中“幸存代”的数量在 JDK11 中要少得多,而且内存使用量在使用率和波动性方面也明显减少。权衡似乎是在速度上,但速度上的差异小于内存使用的差异。
推荐阅读
- javascript - setInterval 仅在间隔结束时执行函数
- android - 将 imageAsset 转换为 base64 (nativescript-vue)
- javascript - jquery-1.7.1.min.js:加载资源失败:服务器响应状态为 404(未找到)
- php - 子串提取。获取最后一个'/'之前的字符串
- r - 如何在R中保持数字逗号分隔和字体更小
- javascript - @babel/preset-env 字段中的“modules:auto”是什么意思?
- flutter - 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” 颤振错误
- python - 解析 netParameter 文件失败
- c# - how to pass mxgraph generated xml to Action of controller?
- php - 如何知道PHP中异常来自的行号