Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Timer question  RSS feed

 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16027
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!