LexManos

Untitled

Jul 10th, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. import java.lang.reflect.Method;
  2. import java.util.Random;
  3.  
  4. public class Comparison
  5. {
  6.     private static final int LOOPS = 5000000;
  7.     public static void main(String[] args) throws Exception
  8.     {
  9.         Method m = Test.class.getDeclaredMethods()[0];
  10.         Test inst = new Test();
  11.         TestBounce bounce = new TestBounce(inst);
  12.         for (int z = 0; z < 10; z++)
  13.         {
  14.             int test = (new Random()).nextInt();
  15.             Stopwatch sw = new Stopwatch();
  16.             sw.start();
  17.             for (int x = 0; x < LOOPS; x++) m.invoke(inst, test);
  18.             sw.stop();
  19.             long ref = sw.length();
  20.             sw.start();
  21.             for (int x = 0; x < LOOPS; x++) bounce.bounce(test);
  22.             sw.stop();
  23.             if (z > 9) //Drops the first few till java gets stabilized and optimized
  24.                 System.out.println(String.format("Ref: %dns Bounce: %dns Factor: %f", ref, sw.length(), (ref / (float)sw.length())));
  25.         }
  26.     }
  27.    
  28.     private static class Test
  29.     {
  30.         public int ret(int a)
  31.         {
  32.             return a;
  33.         }
  34.     }
  35.    
  36.     private static class TestBounce
  37.     {
  38.         Test instance;
  39.         public TestBounce(Test inst)
  40.         {
  41.             instance = inst;
  42.         }
  43.         public int bounce(int a)
  44.         {
  45.             return instance.ret(a);
  46.         }
  47.     }
  48.    
  49.     private static class Stopwatch
  50.     {
  51.         public void start(){
  52.             if (fIsRunning) throw new IllegalStateException("Must stop before calling start again.");
  53.             fStart = System.nanoTime();
  54.             fStop = 0;
  55.             fIsRunning = true;
  56.         }
  57.         public void stop() {
  58.             if (!fIsRunning) throw new IllegalStateException("Cannot stop if not currently running.");
  59.             fStop = System.nanoTime();
  60.             fIsRunning = false;
  61.         }
  62.        
  63.         public String toString(){ return (fStop - fStart) + " ns"; }
  64.         public long length() { return fStop - fStart; }
  65.         private long fStart;
  66.         private long fStop;
  67.         private boolean fIsRunning;
  68.     }
  69.  
  70. }
  71.  
  72. Ref: 2133065816ns Bounce: 340ns Factor: 6273723.000000
  73. Ref: 2191885129ns Bounce: 4417ns Factor: 496238.406250
  74. Ref: 1918747654ns Bounce: 4417ns Factor: 434400.656250
  75. Ref: 2001886097ns Bounce: 340ns Factor: 5887900.000000
  76. Ref: 2098847310ns Bounce: 340ns Factor: 6173080.500000
  77. Ref: 2042958566ns Bounce: 340ns Factor: 6008701.500000
  78. Ref: 2032499240ns Bounce: 680ns Factor: 2988969.500000
  79. Ref: 2213895821ns Bounce: 340ns Factor: 6511458.500000
  80. Ref: 2075877429ns Bounce: 339ns Factor: 6123532.000000
  81. Ref: 2163176430ns Bounce: 339ns Factor: 6381051.500000
Advertisement
Add Comment
Please, Sign In to add comment