我看一下JMH工具。
在示例文件JMHSample_03_States中,他们说:
============================== HOW TO RUN THIS TEST: ====================================
You are expected to see the drastic difference in shared and unshared cases,
because you either contend for single memory location, or not. This effect
is more articulated on large machines.
You can run this test:
a) Via the command line:
$ mvn clean install
$ java -jar target/benchmarks.jar JMHSample_03 -wi 5 -i 5 -t 4 -f 1
(we requested 5 measurement/warmup iterations, with 4 threads, single fork)
我的结果是java 1.8.20.0和Intel i5-4670K CPU @ 3.40GHz
# Run complete. Total time: 00:00:24
Benchmark Mode Samples Score Score error Units
o.s.JMHSample_03_States.measureShared thrpt 5 1547894580.996 3632128.044 ops/s
o.s.JMHSample_03_States.measureUnshared thrpt 5 1571371998.011 6150584.658 ops/s
什么是"剧烈的差异"? measureShared(1547894580)和measureUnshared(1571371998)之间的差异小于2%