Skip to content

Instantly share code, notes, and snippets.

@viossat
Created February 7, 2019 13:00
Show Gist options
  • Save viossat/f2f57ae5458d9c125ee7fd613abc3876 to your computer and use it in GitHub Desktop.
Save viossat/f2f57ae5458d9c125ee7fd613abc3876 to your computer and use it in GitHub Desktop.
package viossat.benchmark;
import net.jpountz.xxhash.XXHash32;
import net.jpountz.xxhash.XXHash64;
import net.jpountz.xxhash.XXHashFactory;
import net.openhft.hashing.LongHashFunction;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) throws Exception {
int n = 1_000_000;
int packetSize = 1653;
Random random = new Random();
List<byte[]> packets = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
byte[] packet = new byte[packetSize];
random.nextBytes(packet);
packets.add(packet);
}
int seed = random.nextInt();
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
XXHash32 xxHash1 = XXHashFactory.fastestInstance().hash32();
XXHash64 xxHash2 = XXHashFactory.fastestInstance().hash64();
LongHashFunction xxHash3 = LongHashFunction.xx();
long t1 = System.currentTimeMillis();
packets.forEach(packet -> {
sha256.digest(packet);
});
long t2 = System.currentTimeMillis();
packets.forEach(packet -> {
xxHash1.hash(packet, 0, packet.length, seed);
});
long t3 = System.currentTimeMillis();
packets.forEach(packet -> {
xxHash2.hash(packet, 0, packet.length, seed);
});
long t4 = System.currentTimeMillis();
packets.forEach(packet -> {
xxHash3.hashBytes(packet);
});
long t5 = System.currentTimeMillis();
System.out.println("java.security.MessageDigest (SHA-256) -> " + (t2 - t1));
System.out.println("net.jpountz.xxhash.XXHash32 -> " + (t3 - t2));
System.out.println("net.jpountz.xxhash.XXHash64 -> " + (t4 - t3));
System.out.println("net.openhft.hashing.LongHashFunction -> " + (t5 - t4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment