问题1:为什么JMH
比简单的System.getNanotime()
更好?
问题2:除了validateLongKeyBinary
比validateLongKeyAscii
快64%,从基准测试结果
部分我能得出什么结论?
示例(代码):
import net.spy.memcached.util.StringUtils;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
public class KeyBench {
private static final String LONG_KEY = "thisIsAFunkyKeyWith_underscores_AndAlso334" +
"3252545345NumberslthisIsAFunkyKeyWith_underscores_AndAlso3343252545345Numbe" +
"rslthisIsAFunkyKeyWith_underscores_AndAlso3343252545345NumberslthisIsAFunkyK" +
"eyWith_underscores_AndAlso3343252545345Numbersl";
@GenerateMicroBenchmark
public void validateLongKeyBinary() {
StringUtils.validateKey(LONG_KEY, true);
}
@GenerateMicroBenchmark
public void validateLongKeyAscii() {
StringUtils.validateKey(LONG_KEY, false);
}
}
基准测试结果
# Running: benchmarks.KeyBench.validateLongKeyAscii
Result : 393,667 ±(95%) 13,985 ±(99%) 20,094 ops/ms
Statistics: (min, avg, max) = (357,445, 393,667, 413,004), stdev = 19,552
Confidence intervals: 95% [379,682, 407,653], 99% [373,573, 413,762]
# Running: benchmarks.KeyBench.validateLongKeyBinary
Result : 644,023 ±(95%) 6,881 ±(99%) 9,887 ops/ms
Statistics: (min, avg, max) = (621,784, 644,023, 654,178), stdev = 9,620
Confidence intervals: 95% [637,142, 650,904], 99% [634,136, 653,910]
Benchmark Mode Thr Count Sec Mean Mean error Units
b.KeyBench.validateLongKeyAscii thrpt 1 10 1 393,667 20,094 ops/ms
b.KeyBench.validateLongKeyBinary thrpt 1 10 1 644,023 9,887 ops/ms
LONG_KEY
声明为静态常量,因为这可能会导致优化问题。只需使用private String
即可。600k 次操作每毫秒相当于 0.6 次操作 / 纳秒,因此每个方法调用使用的 CPU 周期少于 5 个(取决于您的处理器)。这似乎有点太快了。 - assyliasSystem.getNanotime()
,为什么我们还要编写微基准测试呢? - VB_