Last active
August 21, 2019 08:03
-
-
Save hooji/d8169fa2349b8a0dc5bc4204aa39e5d7 to your computer and use it in GitHub Desktop.
This file contains 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
package com.u7.test; | |
import com.u7.util.*; | |
import java.security.*; | |
import java.util.*; | |
public class RandomSpeedTest | |
{ | |
static int seed = 1234; | |
public static void main(final String[] args) | |
{ | |
gg.sleepSeconds(2); | |
speedTest("Regular Random", new Runnable() | |
{ | |
@Override | |
public void run() | |
{ | |
Random r = new Random(seed++); | |
byte [] b = new byte[64]; | |
r.nextBytes(b); | |
} | |
}); | |
speedTest("SecureRandom.getInstanceStrong", new Runnable() | |
{ | |
@Override | |
public void run() | |
{ | |
Random r = null; | |
try | |
{ | |
r = SecureRandom.getInstanceStrong(); | |
} | |
catch(NoSuchAlgorithmException e) | |
{ | |
throw new Error(e); | |
} | |
byte [] b = new byte[64]; | |
r.nextBytes(b); | |
} | |
}); | |
speedTest("SecureRandom.getInstance(\"SHA1PRNG\")", new Runnable() | |
{ | |
@Override | |
public void run() | |
{ | |
Random r = null; | |
try | |
{ | |
r = SecureRandom.getInstance("SHA1PRNG"); | |
r.setSeed(seed++); | |
} | |
catch(NoSuchAlgorithmException e) | |
{ | |
throw new Error(e); | |
} | |
byte [] b = new byte[64]; | |
r.nextBytes(b); | |
} | |
}); | |
} | |
private static void speedTest(final String msg, final Runnable r) | |
{ | |
int n = 10; | |
p("About to test: " + msg); | |
Stopwatch s = new Stopwatch(true); | |
for(int i = 0; i < n; ++i) | |
{ | |
r.run(); | |
} | |
p("Test complete for " + msg); | |
p(s.generateReport(n)); | |
p(""); | |
} | |
public static void p(String s) | |
{ | |
System.out.println(s); | |
System.out.flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment