Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I capture milliseconds in perl? Can anybody help me please!!

Replies are listed 'Best First'.
Re: Milliseconds
by chipmunk (Parson) on Feb 06, 2001 at 21:54 UTC
      Oooh! Oooh! Don't forget select(undef, undef, undef, $ms); for sleeping for minute amounts of time. (sleep() sleeps for 1 or more seconds, and (depending on what platform) will sleep either more or less than you want it to.)

      redmist
      Silicon Cowboy
      redmist::webnode
Re: Milliseconds
by Fastolfe (Vicar) on Feb 06, 2001 at 21:54 UTC
Re: Milliseconds
by AgentM (Curate) on Feb 06, 2001 at 21:57 UTC
    I'm not sure what you mean by "capture" by you can certainly wait for a specified very small amount of time depending on your system's time resolution. If you wish to wait for 10 ms, then:
    use Curses; ... napms(10);

    Depending on your system resolution, this may or may not wait ten seconds, in which case, it will round up to the next time resolution allowed. For modern systems, it's most likely that this will work well.

    Unfortunately, if you wish to interrupt a command after 10 ms, this is nearly impossible without threads (which Perl doesn't manage reliably) and alarm has second resolution only. For reading in from file descriptors, look in select (the non-Perl C-ish version which allows you to wait a specified amount of time for incoming data.)

    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Milliseconds
by Anonymous Monk on Feb 07, 2001 at 20:58 UTC
    Thanks guys... what I really want to do is calculate generation time for my web page in milliseconds.
      Then you want as mentioned by Fastolfe, Time::HiRes, with code something like this:
      use Time::HiRes qw(gettimeofday); $th0 = gettimeofday; #your code goes here $th1 = gettimeofday; print "<P>Script took: ", sprintf ("%5.3f",($th1 - $th0)), " seconds t +o the millisecond att www.yoursite.com";

      /jeorgen

        If one uses un*x, a 'time perl script' would give time in millisecs as well.

        Jeroen
        "We are not alone"(FZ)