???????????
?????????? distance() ????????????????????????Java????????????????????????OpenJDK????JMH??Java Microbenchmark Harness?????????????????????????????????????????????????????????????????????????????????
????@State(Scope.Benchmark)
????public class MyBenchmark {
????private Levenshtein lev = new Levenshtein<>(StringAsList::new);
????@Benchmark
????@BenchmarkMode(Mode.AverageTime)
????@OutputTimeUnit(TimeUnit.NANOSECONDS)
????public int timeLevenshtein() {
????return lev.distance("autoboxing is fast"?? "autoboxing is slow");
????}
????}
???????????????????????JMH???????Щ?????????????????????????????????????????????????
??????????????
$ java -jar target/benchmarks.jar -f 1 -wi 8 -i 8
# JMH 1.10.2 (released 3 days ago)
# VM invoker: /usr/lib/jvm/java-8-openjdk/jre/bin/java
# VM options:
# Warmup: 8 iterations?? 1 s each
# Measurement: 8 iterations?? 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread?? will synchronize iterations
# Benchmark mode: Average time?? time/op
# Benchmark: com.tavianator.boxperf.MyBenchmark.timeLevenshtein
# Run progress: 0.00% complete?? ETA 00:00:16
# Fork: 1 of 1
# Warmup Iteration 1: 1517.495 ns/op
# Warmup Iteration 2: 1503.096 ns/op
# Warmup Iteration 3: 1402.069 ns/op
# Warmup Iteration 4: 1480.584 ns/op
# Warmup Iteration 5: 1385.345 ns/op
# Warmup Iteration 6: 1474.657 ns/op
# Warmup Iteration 7: 1436.749 ns/op
# Warmup Iteration 8: 1463.526 ns/op
Iteration 1: 1446.033 ns/op
Iteration 2: 1420.199 ns/op
Iteration 3: 1383.017 ns/op
Iteration 4: 1443.775 ns/op
Iteration 5: 1393.142 ns/op
Iteration 6: 1393.313 ns/op
Iteration 7: 1459.974 ns/op
Iteration 8: 1456.233 ns/op
Result "timeLevenshtein":
1424.461 ±(99.9%) 59.574 ns/op [Average]
(min?? avg?? max) = (1383.017?? 1424.461?? 1459.974)?? stdev = 31.158
CI (99.9%): [1364.887?? 1484.034] (assumes normal distribution)
# Run complete. Total time: 00:00:16
Benchmark Mode Cnt Score Error Units
MyBenchmark.timeLevenshtein avgt 8 1424.461 ± 59.574 ns/op
????????
??????????????·????hot path?????????JMH??????Linux????perf??????????????JIT??????????????????????????hsdis?????????AUR???????????Arch??????????????????JMH????????? -prof perfasm ????????????????
????$ java -jar target/benchmarks.jar -f 1 -wi 8 -i 8 -prof perfasm
????...
????cmp $0x7f??%eax
????jg 0x00007fde989a6148 ;*if_icmpgt
????; - java.lang.Character::valueOf@3 (line 4570)
????; - com.tavianator.boxperf.StringAsList::get@8 (line 14)
????; - com.tavianator.boxperf.StringAsList::get@2; (line 5)
????; - com.tavianator.boxperf.Levenshtein::distance@121 (line 32)
????cmp $0x80??%eax
????jae 0x00007fde989a6103 ;*aaload
????; - java.lang.Character::valueOf @ 10 (line 4571)
????; - com.tavianator.boxperf.StringAsList::get@8 (line 14)
????; - com.tavianator.boxperf.StringAsList::get @ 2 (line 5)
????; - com.tavianator.boxperf.Levenshtein::distance@121 (line 32)
????...
???????????????????????????????????б????????????0x7f/0×80?????????????????????Character.valueOf()?????????
????private static class CharacterCache {
????private CharacterCache(){}
????static final Character cache[] = new Character[127 + 1];
????static {
????for (int i = 0; i < cache.length; i++)
????cache[i] = new Character((char)i);
????}
????}
????public static Character valueOf(char c) {
????if (c return CharacterCache.cache[(int)c];
????}
????return new Character(c);
????}
?????????????Java??????漲?127??char??Character????????????У?Character.valueOf()??????????????????????????????????????????????????????????????????????????????????????????????????????JVM?????? Character.valueOf(c).charValue() == c???????????????????????????JVM??????????????Character????????????????????????? c ??????????
???????????
????????????????
????@ @ -11??7 +11??7 @ @ public class StringAsList extends AbstractList {
????@Override
????public Character get(int index) {
????- return str.charAt(index); // Autoboxing!
????+ return new Character(str.charAt(index));
????}
????@Override
????????????????????????????????Character.valueOf()??????JVM????????????
????private final char value;
????public Character(char value) {
????this.value = value;
????}
????public char charValue() {
????return value;
????}
????????????м??????????????JVM????????????壬??????String?л??char????????????????????
????$ java -jar target/benchmarks.jar -f 1 -wi 8 -i 8
????...
????# Run complete. Total time: 00:00:16
????Benchmark Mode Cnt Score Error Units
????MyBenchmark.timeLevenshtein avgt 8 1221.151 ± 58.878 ns/op
?????????????14%???? -prof perfasm ????????????????????????String?????char??????????б????
????movzwl 0x10(%rsi??%rdx??2)??%r11d ;*caload
????; - java.lang.String::charAt@27 (line 648)
????; - com.tavianator.boxperf.StringAsList::get@9 (line 14)
????; - com.tavianator.boxperf.StringAsList::get @ 2 (line 5)
????; - com.tavianator.boxperf.Levenshtein::distance@121 (line 32)
????cmp %r11d??%r10d
????je 0x00007faa8d404792 ;*if_icmpne
????; - java.lang.Character::equals@18 (line 4621)
????; - com.tavianator.boxperf.Levenshtein::distance@137 (line 33)
???????
?????????HotSpot?????????????????????????á??????????????????????壬???????????????????????????б?????
????????????????????????GitHub??????