I need to get the execution time for a function in Java. I am aware of two methods I can use:
currentTimeMillis();
and nanoTime();
But I have learned that currentTimeMillis();
is more accurate if I need the wall-clock time (i.e, as if I am measuring from the wall-clock how much time the execution took. Not the processing time).
But currentTimeMillis();
can not give me small fraction number. For example, if the execution time is less than 1 millisecond, it returns 0. I need the time even if it is less than 1, say, 0.05 ms. This is a simple example when the method returns 0.
long startTime=System.currentTimeMillis();
for(int x=0; x<10;x++)
{
System.out.println("-");
}
long execTime=System.currentTimeMillis() - startTime;
Even if it returns time, it return it as 30 or 40. But, I need a more precise number, say, 30.00012. Also, the method return type is long
but I changed it to double
as I want the number in a floating point, is there any harm in this? Can you tell me to the proper way by which I can measure my java method execution wall-clock time in small fraction number (e.g. not 8.0 but 8.287335)