官术网_书友最值得收藏!

Benchmark time units

If we want to get the result in a more readable format, we can specify a time unit. In our case, it would be better to use NANOSECONDS. To do so, we can rewrite our method as follows:

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void testMethod() {
int a = 3;
int b = 4;
int c = a + b;
}

The output will now be:

 # Run progress: 0.00% complete, ETA 00:00:10
# Fork: 1 of 1
# Warmup Iteration 1: 0.345 ns/op
# Warmup Iteration 2: 0.342 ns/op
# Warmup Iteration 3: 0.342 ns/op
# Warmup Iteration 4: 0.337 ns/op
# Warmup Iteration 5: 0.342 ns/op
Iteration 1: 0.336 ns/op
Iteration 2: 0.333 ns/op
Iteration 3: 0.342 ns/op
Iteration 4: 0.340 ns/op
Iteration 5: 0.340 ns/op

Result "org.sample.MyBenchmark.testMethod":
0.338 ±(99.9%) 0.015 ns/op [Average]
(min, avg, max) = (0.333, 0.338, 0.342), stdev = 0.004
CI (99.9%): [0.323, 0.353] (assumes normal distribution)
# Run complete. Total time: 00:00:10

Benchmark Mode Cnt Score Error Units
MyBenchmark.testMethod avgt 5 0.338 ± 0.015 ns/op

0.338 ns/op result is much easier to interpret than 10-9 s/op. You can specify any TimeUnit:

  • NANOSECONDS
  • MICROSECONDS
  • MILLISECONDS
  • SECONDS
  • MINUTES
  • HOURS
  • DAYS

Let's suppose that our variables, a and b, require some time for initialization or are members of some global state. How can we share them between several benchmark methods? How can we initialize them before a scope of a benchmark method? For this, we can use a benchmark state.

主站蜘蛛池模板: 车致| 彭州市| 宣威市| 偃师市| 彭泽县| 蓬莱市| 清远市| 信阳市| 庆元县| 双城市| 临颍县| 准格尔旗| 新巴尔虎左旗| 商河县| 冀州市| 克什克腾旗| 富阳市| 望都县| 云安县| 司法| 姜堰市| 德格县| 铜陵市| 美姑县| 六枝特区| 闵行区| 运城市| 平昌县| 库尔勒市| 郑州市| 遂平县| 裕民县| 宁河县| 满洲里市| 六枝特区| 衡阳市| 林州市| 绩溪县| 沂水县| 渑池县| 额济纳旗|