File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to calculate excuteion time for a line of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to calculate excuteion time for a line of code" Watch "how to calculate excuteion time for a line of code" New topic
Author

how to calculate excuteion time for a line of code

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 550
Thank you for reading my post
Is there any way to calculate the time that an statement take to execute?
I want the time to be calculated in MIcrosecond not in millisecods.
is there any way to do this ?


Thanks
Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
I have never tried such a thing but, could you do:

long start = System.nanoTime();
MyObject.run();
long total = System.nanoTime() - start;

Then if you want to convert the time, multiply or divide by whatever constant you need for the conversion?

You could probably do the same in milliseconds with System.currentTimeMillis().
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
This is only feasible if the line that you want to time is going to take quite a while to execute. By quite a while, I guess I mean tens of milliseconds. For most single lines of code, you have no chance of getting an accurate timing, because they execute very fast and the timing is not that accurate; often the timing code would actually take longer to run than the single line you're trying to time!

Note that nanoTime() does not necessarily get you what you want. Sure, it has nanosecond precision, but that does not mean it has nanosecond accuracy. Consider a cheap digital stop-watch. It may well display the runner's time to hundredths of a second, but the inaccuracy of the watch, combined with the difficulty of pressing the buttons at exactly the right time, means that the accuracy will never be better than a few tenths of a second.

Perhaps one should ask what you wish to achieve by timing a single statement?
[ February 27, 2006: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you talk yourself out of microseconds, see JAMon. The author hangs around the ranch.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to calculate excuteion time for a line of code
 
Similar Threads
How to test JSP performance
calculate time elapsed using javascript?
Web page size & response time calculation
How to access the String litral pool?
Network Traffic