开发者

How to estimate execution time of method when it is invoked using Java Reflection

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.

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[开发者_C百科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();")


If you want to measure times with some precision, you must use System.nanoTime(). Invoke as the very first line of the addMe() and invoke again as the last line, the compute the difference and print it, remember, they are nanoseconds.

If you want to measure the execution time from outside (in the main) without including the invoke() overhead, you simply can't.

System.currentTimeMillis() is very fast to execute compared to System.nanoTime(), but has a poor precision (several milliseconds), so be careful if the addMe() executes fast as the measured time could not be relevant. When you want to measure the execution time of a method that is very fast, normally you execute the method thousands in a loop and measure the total execution time with only two calls to System.nanoTime(), as the overhead imposed of the time measurement could be larger than the execution time of the method itself.


To check performance issues you would usually use profilers, then make several variations of you algorithm and compare their performance in the profiler. This is one of the main purposes of profilers, I would say it is much better bet than to try to come with a home grown solution which may or may not give you reliable results. There are quite a few of them, I have very good experience with YourKit.

And - I think a profiler is a must have tool for any serious developer regardless the programming language anyway...


The only way to get the time without reflections is to call the method without reflections. This is because the method is so trivial, that attempting to monitor it will create more overhead than the actual method and give you how fast you can monitor the method rather than the speed of the method.

public static int addMe(int a, int b) {
    return a + b;
}

public static void main(String args[]) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
    Method addMe = CustomeMath.class.getMethod("addMe", int.class, int.class);
    int result = (Integer) addMe.invoke(null, 37, 47);
    System.out.println(result);

    int runs = 10000000;
    {
        long start = System.nanoTime();
        for (int i = 0; i < runs; i++)
            addMe(i, 10);
        long time = System.nanoTime() - start;
        System.out.printf("The average call time was %.1f ns%n", (double) time / runs);
    }
    {
        long start = System.nanoTime();
        for (int i = 0; i < runs; i++)
            addMe.invoke(null, i, 10);
        long time = System.nanoTime() - start;
        System.out.printf("The average call time using reflections was %.1f ns%n", (double) time / runs);
    }
}

prints

84
The average call time was 1.4 ns
The average call time using reflections was 670.1 ns

Some homework for you;

  • Can you can speculate why there is such a difference?
  • Why might you get a different result in a more realistic example?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜