Click here to Skip to main content
15,888,020 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

Can anyone help me in this issue - I am invoking a method "add" of class "CustomeMath". Now, I want to know the the execution time taken by the method addMe() only (This time does not include the execution time taken by method "Invoke()" of reflection to invoke some method.) I am posting the generic code of this problem.

Java
import java.lang.reflect.*;
       
   public class CustomeMath{
     public int addMe(int a, int b)
      {
         return a + b;
      }
        
      public static void main(String args[])
      {
         try {
           Class cls = Class.forName("CustomeMath");
           Class partypes[] = new Class[2];
            partypes[0] = Integer.TYPE;
            partypes[1] = Integer.TYPE;
            Method meth = cls.getMethod(
              "addMe", partypes);
            CustomeMath methobj = new CustomeMath();
            Object arglist[] = new Object[2];
            arglist[0] = new Integer(37);
            arglist[1] = new Integer(47);
            Object retobj 
              = meth.invoke(methobj, arglist);
            Integer retval = (Integer)retobj;
            System.out.println(retval.intValue());
         }
         catch (Throwable e) {
            System.err.println(e);         }
      }
   }

Now, I want to get the execution time taken by the method "addMe()" and this time doesn't include time taken by the "invoke()" method.

How do I get this time?(Remember I dont want to use the "System.currentTimeMillis();")
Posted
Updated 6-Apr-11 20:42pm
v2

1 solution

Strictly speaking, you cannot calculate execution of arbitrary code based on static analysis of this code. In general case, it is even impossible to say if this time is infinite or not (well known halting problem proven to be algorithmically undecidable). So, this is even theoretically impossible.

In some special cases of very simple code you can make some estimates based on preliminary measurements of timing on your systems. You know: calculate number of cycles and operations of different type multiplied by operation time and so long. The result will depend on the system though. You could try some different typical systems to get an idea on the differences. This is cannot be very reliable, anyway.

OK, what's the purpose of all that?

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900