问题1:为什么JMH
比简单更好System.getNanotime()
?
问题 2:我可以从结果中得出什么结论(看benchmarking results
部分)除了validateLongKeyBinary
比 快 64%validateLongKeyAscii
吗?
示例(代码):
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