how to get the ACTUAL execution time of a "method" using Java Reflection?
Hi can anyone help me in this issue
>I am executing a method of a Java class using Java Reflection.Now i want to get the real execution time of a method regardless the time taken by reflection.
class AClass { //method
void abc(){ System.out.println("Hello World"); }
//Now using reflection i am going to invoke this method
AClass aObj=new AClass();
Class _cObj=Class.forName("AClass"); Method _meth_Invoke=cObj.getMethod("abc",开发者_JAVA技巧null);
//Now hear as u see that if i take time Stamp before executing this (Method.invoke())statement and then after take another time stamp and subtract it(using "System.nanoTime()" ), then i get the TIME of both methods abc()+ invoke() method.And thats the problem that i just want to get know the actual execution time of Method abc().
_meth_Invoke.invoke(aObj,...); }
Kindly guide me i spent a lot of time on surfing but cant fine the right material for it.
Use a profiler to find out how much time the program is spending executing the method. If you're using Oracle's JDK 6 (or newer), you could use JVisualVM which is included with the JDK for profiling.
Start it by entering the command jvisualvm
in a command prompt window.
The cost of calling a Method via reflection is typically sub-microsecond. You can benchmark this on your machine to confirm this. If you are trying to benchmark a method which is less than 10 micro-seconds this is very hard to do accurately. If its much more than this I wouldn't worry about it.
You can measure the time it takes to call an empty method (void xyz() {}
) via reflection and subtract it from the time you measured calling the abc()
method via reflection.
This is as accurate as you can get programmatically.
Otherwise you have to use a profiler like JVisualVM.
精彩评论