aspose file tools
The moose likes Beginning Java and the fly likes Timer question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Timer question" Watch "Timer question" New topic
Author

Timer question

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
Hello,
i have a question.
why is it that the timer gives out different times for the same number of listmembers (i.e. for e.g: if i ask the programme to sort say 105 elements, it could tell me it takes 0,0016 milliseconds,...and then 0,0 milliseconds...etc)

is this normal?

here's the timer code:




this class sorts and then gives out the sorted list as well as the time used in sorting:



will appreciate any tips as to why i keep on getting different times.

regards

W.O.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 244
The time is too short to measure it accurately.

You can't measure 3ms if your clock ticks each 10ms. And currentTimeMillis() may tick even slower. (On Windows it seems to have about 16ms accuracy.)


The solution is to repeat the task many times and measure total time.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6601
    
    1

On Windows it seems to have about 16ms accuracy


Yep ! I had a lot of trouble with this. You can use the new method in java 5 that measures time in nano seconds - nanoTime()

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html

Or you can use the timing framework and get good timing resolution across all platforms.

https://timingframework.dev.java.net/

I used the timing framework and I found it to be very good. Quite accurate.


SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

The system timer, of which you can get the value using System.currentTimeMillis(), is not very accurate. The accuracy of the timer depends on the operating system, and especially on Windows it's only accurate to about 55 ms.

So you can't use that timer to very accurately time very fast operations.

For accurate timing, try using System.nanoTime(). This method exists in Java 5.0 and newer. From the API docs: "Returns the current value of the most precise available system timer, in nanoseconds."


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

Joined: Nov 07, 2006
Posts: 65
I used this approach once to measure performance and my experience was !!!

It is an extremely crude & error-prone method. The time will vary even if your timing methods can precisely measure upto a few nanoseconds. This is because, while your program is running, the operating-system will be doing a million other tasks at the same time. This will add a lot of error to the time measured.
Try changing the code by removing the method call over-head; this may change the time too! Then try running the program side-by-side with a dozen browser windows & heavy apps. This will give a longer time value. Then, close the programs & measure the time. You will never get the same value.
I also tried calculating the mean of the time values, but, the error & uncertainity in the final result are way too much! So, I would discourage the use of such an algorithm for measuring performance/speed/time taken.

Best regards,
Abdul Rehman.
[ November 23, 2006: Message edited by: Abdul Rehman ]

SCJP 5.0 (100%)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Timer question
 
Similar Threads
time of execution
using Iterator or without using Iterator
Returning object and returning integer
please delete this post
Help with timer