brainfrz

Untitled

Oct 3rd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. public class BenchSimpRad
  2. {
  3.     public static final int TEST_VALUE = 1274912425; // This will simpilify to 205 root 30337
  4.  
  5.  
  6.     static long[] simplify2( long square )
  7.     {
  8.         double outside = 1;
  9.         double inside = square;
  10.         long[] simplified = { 1, square };
  11.  
  12.         // Check if it's already a perfect square
  13.         outside = Math.sqrt(square);
  14.         if (outside == Math.floor(outside))
  15.         {
  16.             simplified[0] = (long)outside;
  17.             simplified[1] = 1;
  18.             return simplified;
  19.         }
  20.        
  21.         // Find all the squares that could be factors and see if they are
  22.         long i, sqr;
  23.         for (i = 2, sqr = 4; sqr <= (square / 2); i++, sqr = (i * i))
  24.         {
  25.             // Is this square a factor?
  26.             double in = (double)square / sqr;
  27.             if (in == Math.floor(in))
  28.             {
  29.                 simplified[0] = i;
  30.                 simplified[1] = (long)in;
  31.             }
  32.         }
  33.        
  34.         // Otherwise, since it hasn't simplified, return the original radical
  35.         return simplified;
  36.     }
  37.  
  38.     static long[] simplify4( long square )
  39.     {
  40.         long outside = 1;
  41.         long inside = square;
  42.         long[] simplified = { 1, square };
  43.  
  44.         // Check if it's already a perfect square
  45.         outside = (long)Math.sqrt(square);
  46.         if ((outside * outside) == square)
  47.         {
  48.             simplified[0] = outside;
  49.             simplified[1] = 1;
  50.             return simplified;
  51.         }
  52.  
  53.         // Find all the squares that could be factors and see if they are
  54.         for (long factor = 2, sqr = 4; sqr <= (square / 2); factor++, sqr = (factor * factor))
  55.         {
  56.             // Is this square a factor?
  57.             if ((square % sqr) == 0)
  58.             {
  59.                 simplified[0] = factor;
  60.                 simplified[1] = square / sqr;
  61.             }
  62.         }
  63.  
  64.         // Otherwise, since it hasn't simplified, return the original radical
  65.         return simplified;
  66.     }
  67.  
  68.  
  69.     public static void benchmark2( int iterations )
  70.     {
  71.         long startTime = System.nanoTime();
  72.        
  73.         for (int i = 0; i < iterations; i++)
  74.         {
  75.             simplify2(TEST_VALUE);
  76.         }
  77.        
  78.         long endTime = System.nanoTime();
  79.         long avgTime = (endTime - startTime) / iterations;
  80.         System.out.println("SimpRad2 took " + avgTime + "ns per iteration on average over "
  81.                                 + iterations + " iterations.");
  82.     }
  83.  
  84.     public static void benchmark4( int iterations )
  85.     {
  86.         long startTime = System.nanoTime();
  87.        
  88.         for (int i = 0; i < iterations; i++)
  89.         {
  90.             simplify4(TEST_VALUE);
  91.         }
  92.        
  93.         long endTime = System.nanoTime();
  94.         long avgTime = (endTime - startTime) / iterations;
  95.         System.out.println("SimpRad4 took " + avgTime + "ns per iteration on average over "
  96.                                 + iterations + " iterations.");
  97.     }
  98.  
  99.     public static void benchmark2JIT( int iterations )
  100.     {
  101.         for (int i = 0; i < 500; i++)
  102.         {
  103.             simplify2(TEST_VALUE);
  104.         }
  105.  
  106.         long startTime = System.nanoTime();
  107.        
  108.         for (int i = 0; i < iterations; i++)
  109.         {
  110.             simplify2(TEST_VALUE);
  111.         }
  112.        
  113.         long endTime = System.nanoTime();
  114.         long avgTime = (endTime - startTime) / iterations;
  115.         System.out.println("SimpRad2 took " + avgTime + "ns per iteration on average over "
  116.                                 + iterations + " iterations.");
  117.     }
  118.  
  119.     public static void benchmark4JIT( int iterations )
  120.     {
  121.         for (int i = 0; i < 500; i++)
  122.         {
  123.             simplify4(TEST_VALUE);
  124.         }
  125.  
  126.         long startTime = System.nanoTime();
  127.        
  128.         for (int i = 0; i < iterations; i++)
  129.         {
  130.             simplify4(TEST_VALUE);
  131.         }
  132.        
  133.         long endTime = System.nanoTime();
  134.         long avgTime = (endTime - startTime) / iterations;
  135.         System.out.println("SimpRad4 took " + avgTime + "ns per iteration on average over "
  136.                                 + iterations + " iterations.");
  137.     }
  138.  
  139.  
  140.  
  141.     public static void main( String[] args )
  142.     {
  143.         // BENCHMARK SIMPRAD2
  144.         // Run 500 iterations to benchmark without JIT
  145.         benchmark2(50);
  146.         benchmark4(50);
  147.  
  148.         // Run another 10000 iterations to benchmark with JIT
  149.         benchmark2JIT(10000);
  150.         benchmark4JIT(10000);
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment