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


In reply to Re: Stopwatch using perl by Flame
in thread Stopwatch using perl by vijay814u

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.