如何使用Java Reflection估算方法的执行时间? [英] How to estimate execution time of a method using Java Reflection?

查看:112
本文介绍了如何使用Java Reflection估算方法的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



谁能在这个问题上帮助我-我正在调用类"CustomeMath"的方法"add".现在,我只想知道方法addMe()花费的执行时间(这个时间不包括反射方法"Invoke()"花费的执行时间来调用某些方法.)这个问题.

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[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);         }
      }
   }


现在,我想获取方法"addMe()"花费的执行时间,而这个时间不包括"invoke()"方法花费的时间.

我该如何获得时间?(请记住,我不想使用"System.currentTimeMillis();")


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();")

推荐答案

严格来说,您无法计算基于关于此代码的静态分析.在一般情况下,甚至不可能说出这个时间是否是无限的(众所周知的halting problem被证明是algorithmically undecidable).因此,从理论上讲这甚至是不可能的.

在某些非常简单的代码的特殊情况下,您可以基于对系统时序的初步测量来进行一些估算.您知道:计算不同类型的循环次数和操作数乘以操作时间,如此之长.结果将取决于系统.您可以尝试一些不同的典型系统来了解差异.无论如何,这不是很可靠.

好,这是什么目的?

—SA
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


这篇关于如何使用Java Reflection估算方法的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆