-2

Consider the following code:

    import java.io.*;
    import java.util.*;

    class parent{
      private static int static_count;
      final void static_display(){
       for(int i=0; i< 1000000; i++){
         Object obj = new Object();
       }
       System.out.println("Static method called"+static_count+"times");    
      }
      public void dynamic_display(){
        System.out.println("implemented in child");
      } 
    }

    class child extends parent{
      private static int dynamic_count;
      public void dynamic_display(){
        for(int i=0; i< 1000000; i++){
          Object obj = new Object();
        }
        System.out.println("dynamic method called"+dynamic_count+"times");    
      }
    }

    class sample{
      public static void main(String args[]){
      parent pnt= new parent();
      parent pnt2=new child();
      //static binding
      long startTime = System.nanoTime();
      pnt.static_display();
      long elapsedTime = System.nanoTime() - startTime;

      System.out.println("Total execution time for static binding in millis: "
            + elapsedTime/1000000);
      //dynamic binding
      long startTime2 = System.nanoTime();
      pnt2.dynamic_display();
      long elapsedTime2 = System.nanoTime() - startTime2;

      System.out.println("Total execution time for dynamic binding in millis: "
            + elapsedTime2/1000000);
     }
    }

When this code is executed i am getting the following answer,

    Static method called0times
    Total execution time for static binding in millis: 11
    dynamic method called0times
    Total execution time for dynamic binding in millis: 9

consider only the execution time in the result.i tried to find the time taken for statically and dynamically binnded method to execute.but according to my answer,dynamic binding is faster than static binding.how is this possible.am i wrong anywhere.

Hemaa mathavan
  • 348
  • 4
  • 15
  • 3
    This kind of benchmark is meaningless. – Dave Newton Jan 10 '13 at 15:59
  • Your method could use improvement. Call them both a few hundred times first and discard the times. Then call them in alternating sequence: not 1000000 of one, then 1000000 of the other; but 1000000 of (first one, then the other). – CPerkins Jan 10 '13 at 16:00
  • Try calling them in the opposite order, you might get "wonderful" results. – Vic Jan 10 '13 at 16:01
  • 2
    http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – assylias Jan 10 '13 at 16:01
  • I would suggest that you read the question linked by assylias. While I admire your willingness to code up a solution and see what happens, you really could use more knowledge about the inner workings of Java, and such knowledge generally does not come from writing Java code. – Aaron Kurtzhals Jan 10 '13 at 16:12
  • i called them in opposite orders and got the timing right...thanks guys – Hemaa mathavan Jan 10 '13 at 16:27

1 Answers1

2

It looks as though all your tests do the runs in the same order, static then dynamic. The JVM may have decided to optimize your code, because of the amount of time it is running, part way through the static test. The static test would get all the cost of optimization, and some benefit. The dynamic code would get the benefit.

This is just one example of the sorts of issues you can hit with this sort of micro-benchmarking of Java code.

If you want meaningful results, take the real program you are working on and test in context. If the difference is too small to measure in actual use, it is too small to outweigh code clarity.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75