http://www.perlmonks.org?node_id=597037


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.


0x596F752068617665206265656E2068657865642E

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!