• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Timing Execution  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a program that determines the efficiency (execution time) of various sorting algorithms. I know that this can be accomplished using the "System.currentTimeMillis()" to record time before and after, then taking the difference between the two. However, the prof suggested that this can be done using the Timer class from the java library. By reading the class description on Sun's website, it seems that this is mainly used for delaying and scheduling the execution of tasks. Can anyone tell me how the Timer class is used to determine execution time of a task?? (Since the prof suggested this approach, i am wondering if it more precise than the previous approach - using System.currentTimeMillis())...
Any reply is appreciated
 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 2 Timer classes: java.util.Timer and javax.swing.Timer. I don't see how either one of these can help you test execution times of algorithms. I would use System.currentTimeMillis() to do it.
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
combine Timer & TimerTask, I think you can try something like

although the idea seems straightforward to me, no guarantee it will work
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The use of a Timer for accurate timing is a flawed approach.
Understand that when a thread is removed from the executing state into the sleep state for a period of time, it is only moved to the ready state after that period of time.
It is up to the thread scheduler to decide when the thread re-enters the executing state, which will be at some arbitrary time.
<rant>
The number of academics in this industry who hand out false advice at a fundamental level disgusts me. I can only hope they stay in academia and don't pollute this industry with fallacies any more than it already is.
</rant>
 
Kristi Hallam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the advice. I think I will stick with the "System.currentTimeMillis();".....as long as I find one approach that works, I am happy and will now on ignore the advice of my prof
This was my first time using this forum, and found it extremely helpful...thank you and keep up the awesome work!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!