JMH是否根据CPU时间或挂钟时间来计算每个时间单位的操作?

问题描述 投票:3回答:1

考虑到JMH的默认用法,我想确定JMH的时间类型基于它的度量:CPU时间或Wall-clock。

我曾尝试研究JMH的官方样本(https://openjdk.java.net/projects/code-tools/jmh/),教程(在Jenkov,Baeldung,Mykong等网站),但并未设法准确地找到此信息(我承认我可能错过了一些文档或常规信息基准)。

例如,在示例35(https://hg.openjdk.java.net/code-tools/jmh/file/99d7b73cf1e3/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_35_Profilers.java)中,我们以输出为例:

        Benchmark                              (type)  Mode  Cnt     Score    Error   Units

          JMHSample_35_Profilers.Maps.test     hashmap  avgt    5  1553.201 ±   6.199   ns/op

          JMHSample_35_Profilers.Maps.test     treemap  avgt    5  5177.065 ± 361.278   ns/op

因此,我想知道分数列是使用Wall-clock时间还是CPU Time计算出来的,以便我可以准确地解释基准测试结果。

java benchmarking jmh cpu-time
1个回答
0
投票

正如我担心的那样,由于挂钟和CPU时间并不是唯一的选择,因此我错过了基本信息并提出了严重的问题。我遵循了Alexandre Cartapanis的建议,并调查了JMH的源代码。我发现生成基准的类BenchmarkGenerator依赖于System.nanoTime()。

[此外,根据另一个问题System.nanoTime() documentationKevin Bourrillion's answer,我得出结论,JMH默认情况下根据一些任意但固定的源来测量时间(就像System.nanoTime()一样。

© www.soinside.com 2019 - 2024. All rights reserved.