Advertisement
Guest User

randspeedtest

a guest
Jan 31st, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1.     public static void main(String[] args) {
  2.        
  3.         long seed = XorshiftGenerator.randomSeed();
  4.         Xorshift64Generator rand1 = new Xorshift64Generator(seed);
  5.         Xorshift64Generator rand2 = new Xorshift64Generator(seed);
  6.         boolean[] bools = new boolean[1 << 20];
  7.         long t0,t1;
  8.        
  9.         t0 = System.nanoTime();
  10.         for(int i = 0; i < (1 << 20); i++){
  11.             bools[i] = rand1.nextBoolean(); // <- return ( next() & 1 ) != 0;
  12.         }
  13.        
  14.         t1 = System.nanoTime();
  15.         System.out.println("Uncached time lapse: " + (t1 - t0)/1000);
  16.        
  17.         t0 = System.nanoTime();
  18.         for(int i = 0; i < (1 << 20); i++){
  19.             bools[i] = rand2.nextBooleanCached(); // <- cached version
  20.         }
  21.        
  22.         t1 = System.nanoTime();
  23.         System.out.println("Cached time lapse: " + (t1 - t0)/1000);
  24.        
  25.         System.out.println();
  26.         System.out.println("Testing if the order matters..:");
  27.        
  28.         seed = XorshiftGenerator.randomSeed();
  29.         rand1 = new Xorshift64Generator(seed);
  30.         rand2 = new Xorshift64Generator(seed);
  31.        
  32.         t0 = System.nanoTime();
  33.         for(int i = 0; i < (1 << 20); i++){
  34.             bools[i] = rand1.nextBooleanCached();
  35.         }
  36.        
  37.         t1 = System.nanoTime();
  38.         System.out.println("Cached time lapse: " + (t1 - t0)/1000);
  39.        
  40.         t0 = System.nanoTime();
  41.         for(int i = 0; i < (1 << 20); i++){
  42.             bools[i] = rand2.nextBoolean();
  43.         }
  44.        
  45.         t1 = System.nanoTime();
  46.         System.out.println("Uncached time lapse: " + (t1 - t0)/1000);
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement