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

Stopwatch using perl

by vijay814u (Initiate)
on Jan 29, 2007 at 06:32 UTC ( [id://597032]=sourcecode: print w/replies, xml ) Need Help??
Category: Perl
Author/Contact Info U.M.Vijay Bhasker
Description: This stop watch has been created using perl script.
$HH=0;
$MM=0;
$SS=0;
$MSS=1;



for ($i=1;$i<=100000;$i++)
{
system "cls";
print "\n\n\n\n";
print"                  STOP WATCH\n";
print"\n\n";
print"            =============================\n";
print "\t\t\t\t";


if($MSS == 10)
    {
      $SS=$SS+1;
      $MSS=0;
    }

if($SS == 60)
    {
      $MM=$MM+1;
      $SS=0;
    }

if($MM == 60)
    {
      $HH=$HH+1;
      $MM=0;
    }



print "$HH : $MM : $SS : $MSS";
print"\n";
print"            =============================\n";
$MSS=$MSS+1;
print "\n";
sleep 0.1;
}
Replies are listed 'Best First'.
Re: Stopwatch using perl
by Flame (Deacon) on Jan 29, 2007 at 08:15 UTC

    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

      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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://597032]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found