package jdk.test; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.State; import java.util.concurrent.ThreadLocalRandom; /** *
 * Results on JDK9, Linux, i7-2600K CPU, JMH args: -f 1 -wi 5 -i 8 -gc true
 *
 * Benchmark                     Mode  Samples         Score  Score error  Units
 * j.t.HashBench2._hashCode     thrpt        8   8308858.217   353019.084  ops/s
 * j.t.HashBench2.hashCode0     thrpt        8   8207337.729   217048.634  ops/s
 * j.t.HashBench2.hashCode1     thrpt        8  13359572.359   345736.675  ops/s
 * j.t.HashBench2.hashCode2     thrpt        8  15310621.202   238369.017  ops/s
 * j.t.HashBench2.hashCode3     thrpt        8  17637944.829   232155.847  ops/s
 * j.t.HashBench2.hashCode3i    thrpt        8  17724181.444   509913.288  ops/s
 * j.t.HashBench2.hashCode3x    thrpt        8   8344128.432   159508.813  ops/s
 * j.t.HashBench2.hashCode4     thrpt        8  16526850.489   969549.448  ops/s
 * j.t.HashBench2.hashCode5     thrpt        8  17567765.554   917934.885  ops/s
 * j.t.HashBench2.hashCode6     thrpt        8  17705074.332   419405.652  ops/s
 * j.t.HashBench2.hashCode7     thrpt        8  18805633.563   209181.299  ops/s
 * j.t.HashBench2.hashCode8     thrpt        8  18300123.201   376681.550  ops/s
 *
 * 
*/ @State(Scope.Benchmark) public class HashBench2 { private final char[] buf; private final String master; public HashBench2() { buf = new char[128]; for (int i = 0; i < buf.length; i++) { buf[i] = (char) ThreadLocalRandom.current().nextInt(1 << 16); } master = new String(buf); } @Benchmark public int _hashCode() { return new String(master).hashCode(); } @Benchmark public int hashCode0() { int h = 0; for (char c : buf) { h = h * 31 + c; } return h; } @Benchmark public int hashCode1() { int h = 0; int i = 0; int l = buf.length - 1; while (i < l) { h = h * (31 * 31) + buf[i] * 31 + buf[i + 1]; i += 2; } if (i < buf.length) { h = h * 31 + buf[i]; } return h; } @Benchmark public int hashCode2() { int h = 0; int i = 0; int l = buf.length - 2; while (i < l) { h = h * (31 * 31 * 31) + buf[i] * (31 * 31) + buf[i + 1] * 31 + buf[i + 2]; i += 3; } while (i < buf.length) { h = h * 31 + buf[i++]; } return h; } @Benchmark public int hashCode3() { int h = 0; int i = 0; int l = buf.length - 3; while (i < l) { h = h * (31 * 31 * 31 * 31) + buf[i] * (31 * 31 * 31) + buf[i + 1] * (31 * 31) + buf[i + 2] * 31 + buf[i + 3]; i += 4; } while (i < buf.length) { h = h * 31 + buf[i++]; } return h; } @Benchmark public int hashCode3i() { int h = 0; int i = 0; int l = buf.length - 3; while (i < l) { h = h * (31 * 31 * 31 * 31) + buf[i++] * (31 * 31 * 31) + buf[i++] * (31 * 31) + buf[i++] * 31 + buf[i++]; } while (i < buf.length) { h = h * 31 + buf[i++]; } return h; } @Benchmark public int hashCode3x() { int h = 0; int i = 0; int l = buf.length - 3; while (i < l) { h = (((h * 31 + buf[i]) * 31 + buf[i + 1]) * 31 + buf[i + 2]) * 31 + buf[i + 3]; i += 4; } while (i < buf.length) { h = h * 31 + buf[i++]; } return h; } @Benchmark public int hashCode4() { int h = 0; int i = 0; int l = buf.length - 4; while (i < l) { h = h * (31 * 31 * 31 * 31 * 31) + buf[i] * (31 * 31 * 31 * 31) + buf[i + 1] * (31 * 31 * 31) + buf[i + 2] * (31 * 31) + buf[i + 3] * 31 + buf[i + 4]; i += 5; } while (i < buf.length) { h = h * 31 + buf[i++]; } return h; } @Benchmark public int hashCode5() { int h = 0; int i = 0; int l = buf.length - 5; while (i < l) { h = h * (31 * 31 * 31 * 31 * 31 * 31) + buf[i] * (31 * 31 * 31 * 31 * 31) + buf[i + 1] * (31 * 31 * 31 * 31) + buf[i + 2] * (31 * 31 * 31) + buf[i + 3] * (31 * 31) + buf[i + 4] * 31 + buf[i + 5]; i += 6; } while (i < buf.length) { h = h * 31 + buf[i++]; } return h; } @Benchmark public int hashCode6() { int h = 0; int i = 0; int l = buf.length - 6; while (i < l) { //noinspection NumericOverflow h = h * (31 * 31 * 31 * 31 * 31 * 31 * 31) + buf[i] * (31 * 31 * 31 * 31 * 31 * 31) + buf[i + 1] * (31 * 31 * 31 * 31 * 31) + buf[i + 2] * (31 * 31 * 31 * 31) + buf[i + 3] * (31 * 31 * 31) + buf[i + 4] * (31 * 31) + buf[i + 5] * 31 + buf[i + 6]; i += 7; } while (i < buf.length) { h = h * 31 + buf[i++]; } return h; } @Benchmark public int hashCode7() { int h = 0; int i = 0; int l = buf.length - 7; while (i < l) { //noinspection NumericOverflow h = h * (31 * 31 * 31 * 31 * 31 * 31 * 31 * 31) + buf[i] * (31 * 31 * 31 * 31 * 31 * 31 * 31) + buf[i + 1] * (31 * 31 * 31 * 31 * 31 * 31) + buf[i + 2] * (31 * 31 * 31 * 31 * 31) + buf[i + 3] * (31 * 31 * 31 * 31) + buf[i + 4] * (31 * 31 * 31) + buf[i + 5] * (31 * 31) + buf[i + 6] * 31 + buf[i + 7]; i += 8; } while (i < buf.length) { h = h * 31 + buf[i++]; } return h; } @Benchmark public int hashCode8() { int h = 0; int i = 0; int l = buf.length - 8; while (i < l) { //noinspection NumericOverflow h = h * (31 * 31 * 31 * 31 * 31 * 31 * 31 * 31 * 31) + buf[i] * (31 * 31 * 31 * 31 * 31 * 31 * 31 * 31) + buf[i + 1] * (31 * 31 * 31 * 31 * 31 * 31 * 31) + buf[i + 2] * (31 * 31 * 31 * 31 * 31 * 31) + buf[i + 3] * (31 * 31 * 31 * 31 * 31) + buf[i + 4] * (31 * 31 * 31 * 31) + buf[i + 5] * (31 * 31 * 31) + buf[i + 6] * (31 * 31) + buf[i + 7] * 31 + buf[i + 8]; i += 9; } while (i < buf.length) { h = h * 31 + buf[i++]; } return h; } }