Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Stopwatch using perl

by Flame (Deacon)
on Jan 29, 2007 at 08:15 UTC ( #597037=note: print w/replies, xml ) Need Help??

in reply to Stopwatch using perl

This looks like a good start, but I'd like to point out that this program will, on most systems, quickly lose accuracy.

The sleep command, at least as of Perl 5.8.8, is not capable of sleeping at resolutions less than a full second. The Time::HiRes module is recommended as an alternative for those needing better accuracy. In this case, I suspect it is rounding the number down to the nearest whole second, and attempting to sleep for that amount of time. You can check this using the return value from sleep, as it returns the number of seconds actually slept.

Because sleep is returning immediately, the speed of your stopwatch is entirely dependent on how fast the user's computer can print to the console. In my specific case I was able to reach 4 minutes in about 25 seconds.

Because you may not know the speed of the user's computer, it's often wise to change your sleep duration based on how long it took to complete the previous task. In the future, you should try storing the current time before each sleep and using it as a base from which you control your sleeping. (This can get complicated very quickly, depending on your goals.)

Note: It appears that you're just getting started, so don't worry if anything I wrote here was confusing, keep it up and you'll catch on quick.


Replies are listed 'Best First'.
Re^2: Stopwatch using perl
by ptum (Priest) on Jan 29, 2007 at 16:54 UTC

    This is an excellent template for responding to a new Monk's first post, in several ways:

    • It begins with an encouraging remark
    • It points out a measurable and reproducible flaw
    • It clearly explains the nature of the error
    • It outlines a way to correct or avoid the problem
    • It ends with another encouraging remark

    Throughout the post, the tone was cheerful, polite and avoided patronization. Thanks for raising the bar, Flame!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://597037]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2018-03-21 09:31 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (265 votes). Check out past polls.