I am trying to measure the execution time for several methods. so I was thinking to make a method instead of duplicate same code many times.
Here is my code:
You are in the correct path, you need to pass the Method object, the target object on which to execute the method and the arguments it takes and then invoke it into your try catch, something like:
private void MeasureExecutionTime(Method m, Object target, Object args)
throws IllegalArgumentException,
IllegalAccessException,
InvocationTargetException
{
long startTime = System.nanoTime();
long endTime;
try
{
m.invoke(target, args);
}
finally
{
endTime = System.nanoTime();
}
long elapsedTime = endTime - startTime;
System.out.println("This takes " + elapsedTime + " ns.");
}