Skip to content

Instantly share code, notes, and snippets.

@leventov
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leventov/7f229e4a0209ddaf9a6c to your computer and use it in GitHub Desktop.
Save leventov/7f229e4a0209ddaf9a6c to your computer and use it in GitHub Desktop.
Zero-allocation-hashing benchmarks
package tests;
import com.google.common.hash.HashFunction;
import net.openhft.chronicle.hash.hashing.Accesses;
import net.openhft.chronicle.hash.hashing.Hasher;
import net.openhft.hashing.LongHashFunction;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
import static net.openhft.hashing.LongHashFunction.city_1_1;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Threads(1)
@Fork(1)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
@State(Scope.Thread)
public class Hashing implements UnsafeConstants {
public static final HashFunction GUAVA = com.google.common.hash.Hashing.murmur3_32();
public static byte[] bytes = new byte[1024 * 16];
@Param({"1", "2", "3", "4", "6", "8", "12", "16", "24", "32", "48", "64", "128", "1024"})
public int keyLen;
@Benchmark
public long city(Hashing st) {
return city_1_1().hashBytes(bytes, 0, st.keyLen);
}
@Benchmark
public long xx(Hashing st) {
return LongHashFunction.xxHash().hashBytes(bytes, 0, st.keyLen);
}
@Benchmark
public long murmur3(Hashing st) {
return Hasher.hash(bytes, Accesses.unsafe(), BYTE_BASE, st.keyLen);
}
@Benchmark
public long murmur3_Zero(Hashing st) {
return LongHashFunction.murmur_3().hashBytes(bytes, 0, st.keyLen);
}
public static final LongHashFunction seededCity = city_1_1(0L);
@Benchmark
public long seededCity(Hashing st) {
return seededCity.hashBytes(bytes, 0, st.keyLen);
}
@Benchmark
public long guava(Hashing st) {
return GUAVA.hashBytes(bytes, 0, st.keyLen).asLong();
}
}
package tests;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public interface UnsafeConstants {
public static final Unsafe U = Inner.U;
static class Inner {
private static final Unsafe U;
static {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
U = (Unsafe) f.get(null);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
public static final long BYTE_SCALE = 1L;
public static final int BYTE_SCALE_SHIFT = 0;
public static final long CHAR_SCALE = 2L;
public static final int CHAR_SCALE_SHIFT = 1;
public static final long SHORT_SCALE = 2L;
public static final int SHORT_SCALE_SHIFT = 1;
public static final long INT_SCALE = 4L;
public static final int INT_SCALE_SHIFT = 2;
public static final long FLOAT_SCALE = 4L;
public static final int FLOAT_SCALE_SHIFT = 2;
public static final long LONG_SCALE = 8L;
public static final int LONG_SCALE_SHIFT = 3;
public static final long DOUBLE_SCALE = 8L;
public static final int DOUBLE_SCALE_SHIFT = 3;
public static final long BYTE_BASE = (long) Unsafe.ARRAY_BYTE_BASE_OFFSET;
public static final long CHAR_BASE = (long) Unsafe.ARRAY_CHAR_BASE_OFFSET;
public static final long SHORT_BASE = (long) Unsafe.ARRAY_SHORT_BASE_OFFSET;
public static final long INT_BASE = (long) Unsafe.ARRAY_INT_BASE_OFFSET;
public static final long FLOAT_BASE = (long) Unsafe.ARRAY_FLOAT_BASE_OFFSET;
public static final long LONG_BASE = (long) Unsafe.ARRAY_LONG_BASE_OFFSET;
public static final long DOUBLE_BASE = (long) Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment