bb.util
Class RandomUtil.UnitTest

java.lang.Object
  extended by bb.util.RandomUtil.UnitTest
Enclosing class:
RandomUtil

public static class RandomUtil.UnitTest
extends Object

See the Overview page of the project's javadocs for a general description of this unit test class.


Nested Class Summary
private static class RandomUtil.UnitTest.SingleThread_MersenneTwisterFast
           
private static class RandomUtil.UnitTest.SingleThread_RandomPerThread
           
 
Constructor Summary
RandomUtil.UnitTest()
           
 
Method Summary
 void benchmark_get()
          Results on 2009-03-16 (2.5 GHz Xeon E5420 desktop, jdk 1.6.0_11 server jvm): n = 128 * 1024 synchronized MersenneTwisterFast: first = 207.439 ns, mean = 22.028 ns (CI deltas: -4.631 ps, +5.274 ps), sd = 159.556 ns (CI deltas: -25.309 ns, +32.037 ns) WARNING: SD VALUES MAY BE INACCURATE unsynchronized MersenneTwisterFast: first = 11.235 ns, mean = 10.949 ns (CI deltas: -2.072 ps, +2.114 ps), sd = 95.789 ns (CI deltas: -12.099 ns, +14.455 ns) WARNING: SD VALUES MAY BE INACCURATE get (unsynchronized MersenneTwisterFast): first = 54.316 ns, mean = 19.574 ns (CI deltas: -4.562 ps, +4.993 ps), sd = 154.575 ns (CI deltas: -21.387 ns, +26.610 ns) WARNING: SD VALUES MAY BE INACCURATE Note: the low time for get is realistic: my BenchmarkDataStructureAccess class measured access times for ThreadLocal at around 12 ns, so add that to the intrinsic 14 ns and you get 26 ns as reported above.
 void test_makeSeed()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomUtil.UnitTest

public RandomUtil.UnitTest()
Method Detail

test_makeSeed

public void test_makeSeed()
                   throws Exception
Throws:
Exception

benchmark_get

public void benchmark_get()
Results on 2009-03-16 (2.5 GHz Xeon E5420 desktop, jdk 1.6.0_11 server jvm):

                        n = 128 * 1024
                                synchronized MersenneTwisterFast: first = 207.439 ns, mean = 22.028 ns (CI deltas: -4.631 ps, +5.274 ps), sd = 159.556 ns (CI deltas: -25.309 ns, +32.037 ns) WARNING: SD VALUES MAY BE INACCURATE
                                unsynchronized MersenneTwisterFast: first = 11.235 ns, mean = 10.949 ns (CI deltas: -2.072 ps, +2.114 ps), sd = 95.789 ns (CI deltas: -12.099 ns, +14.455 ns) WARNING: SD VALUES MAY BE INACCURATE
                                get (unsynchronized MersenneTwisterFast): first = 54.316 ns, mean = 19.574 ns (CI deltas: -4.562 ps, +4.993 ps), sd = 154.575 ns (CI deltas: -21.387 ns, +26.610 ns) WARNING: SD VALUES MAY BE INACCURATE
 

Note: the low time for get is realistic: my BenchmarkDataStructureAccess class measured access times for ThreadLocal at around 12 ns, so add that to the intrinsic 14 ns and you get 26 ns as reported above.

Conclusions: 1) in a known single threaded environment, should always use a dedicated MersenneTwisterFast instance 2) but the moment multiple threads are involved, you are better off using get than using a synchronized class, and this is true even without the thread contention that is present in highly concurrent situations which makes the case for going with ThreadLocal even stronger.