Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Method;
- import java.util.Random;
- public class Comparison
- {
- private static final int LOOPS = 5000000;
- public static void main(String[] args) throws Exception
- {
- Method m = Test.class.getDeclaredMethods()[0];
- Test inst = new Test();
- TestBounce bounce = new TestBounce(inst);
- for (int z = 0; z < 10; z++)
- {
- int test = (new Random()).nextInt();
- Stopwatch sw = new Stopwatch();
- sw.start();
- for (int x = 0; x < LOOPS; x++) m.invoke(inst, test);
- sw.stop();
- long ref = sw.length();
- sw.start();
- for (int x = 0; x < LOOPS; x++) bounce.bounce(test);
- sw.stop();
- if (z > 9) //Drops the first few till java gets stabilized and optimized
- System.out.println(String.format("Ref: %dns Bounce: %dns Factor: %f", ref, sw.length(), (ref / (float)sw.length())));
- }
- }
- private static class Test
- {
- public int ret(int a)
- {
- return a;
- }
- }
- private static class TestBounce
- {
- Test instance;
- public TestBounce(Test inst)
- {
- instance = inst;
- }
- public int bounce(int a)
- {
- return instance.ret(a);
- }
- }
- private static class Stopwatch
- {
- public void start(){
- if (fIsRunning) throw new IllegalStateException("Must stop before calling start again.");
- fStart = System.nanoTime();
- fStop = 0;
- fIsRunning = true;
- }
- public void stop() {
- if (!fIsRunning) throw new IllegalStateException("Cannot stop if not currently running.");
- fStop = System.nanoTime();
- fIsRunning = false;
- }
- public String toString(){ return (fStop - fStart) + " ns"; }
- public long length() { return fStop - fStart; }
- private long fStart;
- private long fStop;
- private boolean fIsRunning;
- }
- }
- Ref: 2133065816ns Bounce: 340ns Factor: 6273723.000000
- Ref: 2191885129ns Bounce: 4417ns Factor: 496238.406250
- Ref: 1918747654ns Bounce: 4417ns Factor: 434400.656250
- Ref: 2001886097ns Bounce: 340ns Factor: 5887900.000000
- Ref: 2098847310ns Bounce: 340ns Factor: 6173080.500000
- Ref: 2042958566ns Bounce: 340ns Factor: 6008701.500000
- Ref: 2032499240ns Bounce: 680ns Factor: 2988969.500000
- Ref: 2213895821ns Bounce: 340ns Factor: 6511458.500000
- Ref: 2075877429ns Bounce: 339ns Factor: 6123532.000000
- Ref: 2163176430ns Bounce: 339ns Factor: 6381051.500000
Advertisement
Add Comment
Please, Sign In to add comment