aspose file tools*
The moose likes Beginning Java and the fly likes Timing Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Timing Methods" Watch "Timing Methods" New topic
Author

Timing Methods

Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
I need to perform the same operation using two different ways of doing it, and I need to show which method was more efficient. I've been told there's a way to time these methods using something like system timers. Does anybody know how this is done? I'm assuming I need to run a line of code before and after each method and compare the results.

groberts1980


Greg Roberts<br />CIS Student<br />University of West Florida
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

The easiest way is to use System.currentTimeMillis()


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

Be aware that System.currentTimeMillis() is not very accurate, and milliseconds can be quite long if you're talking about code running on a computer. So the currentTimeMillis() method will only really be useful if the methods take a relatively long time to execute.

If you are using Java 5, you could try using System.nanoTime(), which might be more accurate (depending on the system you're using!). See the API documentation.

Also, be aware that the Java VM might compile your byte code to native code using the JIT, so if you call the same method multiple times, it might run faster the second, third etc. time than the first time. Ideally, you should run each method that you want to test a large number of times and calculate the average execution time:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
If the operations you're comparing complete so quickly that you can't see the difference at millisecond granularity, try running them multiple times (say, 10,000 times) in loops & compare the cumulative results.
Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
I'm getting an error that I can't seem to trace. In the code:


I'm getting "Unreachable code" on endTime = System.nanoTime(); Why is this code unreachable?

P.S. This is part of a program to generate a Fibonacci number. The above code is from the method using iteration.
[ November 18, 2005: Message edited by: Greg Roberts ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Stare at these two lines!


There is no emoticon for what I am feeling!
Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
Yeah, I guess it would help if the system time method was called before the return statement, wouldn't it?

I've got another problem. I'm getting stack overflow errors with my recursive method:



Its a program to compute Fibonacci numbers, but I've never programmed with recursion before. Can anybody tell me where I'm going wrong here?
[ November 19, 2005: Message edited by: Greg Roberts ]
 
 
subject: Timing Methods