Skip to content

Commit

Permalink
Add Fingerprint2011 to ChecksumBenchmark
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 686542171
  • Loading branch information
java-team-github-bot authored and Google Java Core Libraries committed Oct 16, 2024
1 parent 5338f7c commit 503c9eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,31 @@ byte adler32Checksum(int reps) throws Exception {
return result;
}

// Fingerprint2011

@Benchmark
byte fingerprintHashFunction(int reps) {
return runHashFunction(reps, Hashing.fingerprint2011());
}

@Benchmark
byte fingerprintChecksum(int reps) throws Exception {
byte result = 0x01;
for (int i = 0; i < reps; i++) {
HashCode checksum = new Fingerprint2011().hashBytes(testBytes, 0, testBytes.length);
result = (byte) (result ^ checksum.asLong());
}
return result;
}

// Helpers + main

private byte runHashFunction(int reps, HashFunction hashFunction) {
byte result = 0x01;
// Trick the JVM to prevent it from using the hash function non-polymorphically
result ^= Hashing.crc32().hashInt(reps).asBytes()[0];
result ^= Hashing.adler32().hashInt(reps).asBytes()[0];
result ^= Hashing.fingerprint2011().hashInt(reps).asBytes()[0];
for (int i = 0; i < reps; i++) {
result ^= hashFunction.hashBytes(testBytes).asBytes()[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,31 @@ byte adler32Checksum(int reps) throws Exception {
return result;
}

// Fingerprint2011

@Benchmark
byte fingerprintHashFunction(int reps) {
return runHashFunction(reps, Hashing.fingerprint2011());
}

@Benchmark
byte fingerprintChecksum(int reps) throws Exception {
byte result = 0x01;
for (int i = 0; i < reps; i++) {
HashCode checksum = new Fingerprint2011().hashBytes(testBytes, 0, testBytes.length);
result = (byte) (result ^ checksum.asLong());
}
return result;
}

// Helpers + main

private byte runHashFunction(int reps, HashFunction hashFunction) {
byte result = 0x01;
// Trick the JVM to prevent it from using the hash function non-polymorphically
result ^= Hashing.crc32().hashInt(reps).asBytes()[0];
result ^= Hashing.adler32().hashInt(reps).asBytes()[0];
result ^= Hashing.fingerprint2011().hashInt(reps).asBytes()[0];
for (int i = 0; i < reps; i++) {
result ^= hashFunction.hashBytes(testBytes).asBytes()[0];
}
Expand Down

0 comments on commit 503c9eb

Please sign in to comment.