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
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>
Tony Morris Java Q&A (FAQ, Trivia)
posted 15 years ago
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!