fork(15) download
  1. import java.io.*;
  2.  
  3. class StaticVsInstanceBenchmark
  4. {
  5. public static void main( String[] args ) throws Exception
  6. {
  7. StaticVsInstanceBenchmark program = new StaticVsInstanceBenchmark();
  8. program.run();
  9. }
  10.  
  11. static final int DURATION = 1000;
  12.  
  13. public void run() throws Exception
  14. {
  15. doBenchmark( new VirtualTest( new ClassWithVirtualMethod() ),
  16. new NonVirtualTest( new ClassWithNonVirtualMethod() ),
  17. new StaticTest() );
  18. }
  19.  
  20. void doBenchmark( Test... tests ) throws Exception
  21. {
  22. System.out.println( " Name | Iterations" );
  23. doBenchmark2( devNull, 1, tests ); //warmup
  24. doBenchmark2( System.out, DURATION, tests );
  25. System.out.println( "Done." );
  26. }
  27.  
  28. void doBenchmark2( PrintStream printStream, int duration, Test[] tests ) throws Exception
  29. {
  30. for( Test test : tests )
  31. {
  32. long iterations = runTest( duration, test );
  33. printStream.printf( "%15s | %10d\n", test.getClass().getSimpleName(), iterations );
  34. }
  35. }
  36.  
  37. long runTest( int duration, Test test ) throws Exception
  38. {
  39. test.terminate = false;
  40. test.count = 0;
  41. Thread thread = new Thread( test );
  42. thread.start();
  43. Thread.sleep( duration );
  44. test.terminate = true;
  45. thread.join();
  46. return test.count;
  47. }
  48.  
  49. static abstract class Test implements Runnable
  50. {
  51. volatile boolean terminate = false;
  52. long count = 0;
  53. }
  54.  
  55. static class ClassWithStaticStuff
  56. {
  57. static int staticDummy;
  58. static void staticMethod() { staticDummy++; }
  59. }
  60.  
  61. static class StaticTest extends Test
  62. {
  63. @Override
  64. public void run()
  65. {
  66. for( count = 0; !terminate; count++ )
  67. {
  68. ClassWithStaticStuff.staticMethod();
  69. }
  70. }
  71. }
  72.  
  73. static class ClassWithVirtualMethod implements Runnable
  74. {
  75. int instanceDummy;
  76. @Override public void run() { instanceDummy++; }
  77. }
  78.  
  79. static class VirtualTest extends Test
  80. {
  81. final Runnable runnable;
  82.  
  83. VirtualTest( Runnable runnable )
  84. {
  85. this.runnable = runnable;
  86. }
  87.  
  88. @Override
  89. public void run()
  90. {
  91. for( count = 0; !terminate; count++ )
  92. {
  93. runnable.run();
  94. }
  95. }
  96. }
  97.  
  98. static class ClassWithNonVirtualMethod
  99. {
  100. int instanceDummy;
  101. final void nonVirtualMethod() { instanceDummy++; }
  102. }
  103.  
  104. static class NonVirtualTest extends Test
  105. {
  106. final ClassWithNonVirtualMethod objectWithNonVirtualMethod;
  107.  
  108. NonVirtualTest( ClassWithNonVirtualMethod objectWithNonVirtualMethod )
  109. {
  110. this.objectWithNonVirtualMethod = objectWithNonVirtualMethod;
  111. }
  112.  
  113. @Override
  114. public void run()
  115. {
  116. for( count = 0; !terminate; count++ )
  117. {
  118. objectWithNonVirtualMethod.nonVirtualMethod();
  119. }
  120. }
  121. }
  122.  
  123. static final PrintStream devNull = new PrintStream( new OutputStream()
  124. {
  125. public void write(int b) {}
  126. } );
  127. }
  128.  
Success #stdin #stdout 3.06s 711168KB
stdin
Standard input is empty
stdout
  Name          |  Iterations
    VirtualTest |  343338494
 NonVirtualTest |  566721054
     StaticTest |  613975425
Done.