File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Execution time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Execution time" Watch "Execution time" New topic
Author

Execution time

chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Hi Guys,

I want to find the time taken by some of the functions in my code. The following is the sample code i have written to compute the time. I wanted to know whether this is the proper way of doing it.



Also, is there a way to find the time in micro seconds?


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 174

Basically your code is correct.
There is also a System.nanoTime() method.
And, your for() loop as written may be optimized away.
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Hey,

Thanks for the prompt reply. Can you expand a bit more on the optimization thing. Probably, some useful resources?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Note that the resolution of currentTimeMillis() is not very good on all operating systems - on some systems the timer goes in steps of 50 ms, which is too long to time quick running methods.

Also note that the JVM does a lot of sophisticated optimizations and that makes timing your code this way very hard. The JVM will most likely throw away the empty loop in your method completely, because it's smart enough to understand that the loop has no effect at all. You then think you are timing the loop, while in reality the loop isn't even run at all.

If you really want to measure the performance of your code, use profiling software instead of trying to do it with a timer yourself. Note that the JDK includes a tool for this, JVisualVM.

Read this article to get an idea of how tricky it is to measure the performance of Java programs:
Dynamic compilation and performance measurement

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Thanks all. Now i understood why i was always getting 0 as the execution time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execution time