Question

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)

Was it helpful?

Solution 3

Although you can and should use nanoTime to get the most precise available execution time for a block of code. However, you should use it with caution, because there are many outside issues that can affect execution time.

For example, any time a method is called without having run recently, there will be overhead for getting it into cache. Depending on what else is in the same page, there may even be a disk read to get it into memory.

A JVM may treat a method differently if it only runs once compared to running it many times.

Generally, when you care about microseconds it is because the thing you are measuring is going to run many times in a job or transaction. To get an accurate measure of that you need to measure across many calls. Doing so also reduces the impact of the overhead time for calling nanoTime.

OTHER TIPS

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).

That's only partly accurate.

Both functions return wall-clock time, and not CPU time.

The difference, other than precision, is that currentTimeMillis() is relative to a well-known point in time, whereas nanoTime() is relative to some arbitrary point in time. This means that you can't easily translate the result of nanoTime() into a timestamp that you and I would understand.

You can still use it to measure wall-clock intervals: just call nanoTime() twice and take the difference. That's pretty much what it's been designed for.

Finally, if you're using this to profile some code, read How do I write a correct micro-benchmark in Java? There are lots of subtleties.

Use System.nanoTime() it has precision of nanosecond.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top