Last active
May 5, 2017 15:18
-
-
Save brettwooldridge/3e470910c1b6b7c13d14c647cfbede70 to your computer and use it in GitHub Desktop.
Cheap Java SHA1 Digest speed test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DigestTest | |
{ | |
@Test | |
public void test() { | |
String sql = "SELECT count(ZDeviceLite.device_id) FROM device ZDeviceLite"; | |
// This variable is meaningless and only used for benchmarking, to prevent the | |
// JVM from optimizing away the loops below due to non-use of the returned types. | |
int hash = 0; | |
// Warm-up the JIT (default C2 compile threshold is 10000 code path executions) | |
for (int i = 0; i < 20000; i++) { | |
hash ^= getSha1Digest().digest(sql.getBytes()).hashCode(); | |
} | |
// Run cheap timed test of 1000 iterations | |
long start = System.nanoTime(); | |
for (int i = 0; i < 1000; i++) { | |
hash ^= getSha1Digest().digest(sql.getBytes()).hashCode(); | |
} | |
System.out.printf("Hash: %d\nTotal ns: %d\n", hash, (System.nanoTime() - start)); | |
} | |
private MessageDigest getSha1Digest() { | |
try { | |
return MessageDigest.getInstance("SHA-1"); | |
} catch (final NoSuchAlgorithmException e) { | |
throw new IllegalArgumentException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: 1000 SHA1 computations in 1714058ns (1.7ms).