Created
March 24, 2015 04:20
-
-
Save moznion/508bd516e0c547e79d29 to your computer and use it in GitHub Desktop.
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
package net.moznion; | |
import me.geso.nanobench.Benchmark; | |
import me.geso.nanobench.Benchmark.Bench; | |
import java.util.Random; | |
import java.util.concurrent.ThreadLocalRandom; | |
public class BenchmarkRandom { | |
public static class RandomNumberGenerator extends Thread { | |
private static final Random RANDOM = new Random(); | |
@Override | |
public void run() { | |
RANDOM.nextDouble(); | |
} | |
} | |
public static class ThreadLocalRandomNumberGenerator extends Thread { | |
@Override | |
public void run() { | |
ThreadLocalRandom.current().nextDouble(); | |
} | |
} | |
@Bench | |
public void withRandom() throws InstantiationException, IllegalAccessException { | |
runThreads(RandomNumberGenerator.class); | |
} | |
@Bench | |
public void withThreadLocalRandom() throws InstantiationException, IllegalAccessException { | |
runThreads(ThreadLocalRandomNumberGenerator.class); | |
} | |
private <E extends Thread> void runThreads(Class<E> threadClass) | |
throws InstantiationException, IllegalAccessException { | |
for (int i = 0; i < 1000; i++) { | |
threadClass.newInstance().start(); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
new Benchmark(new App()).warmup(10).run(1000).timethese().cmpthese(); | |
} | |
} |
Author
moznion
commented
Mar 24, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment