Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Timer implementation

by bash (Scribe)
on Sep 16, 2008 at 02:06 UTC ( [id://711582]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Gurus, I am writing simple timer for my perl script and got confusion. My assumption was that Real Time = USER+SYSTEM time, but this is not true. For example:
$ time ls real 0m0.006s user 0m0.001s sys 0m0.004s real > user+sys
How it can be? I want to understand this situation, because of this depends what to use - perl built-in times() or Time::HiRes::gettimeofday functions.

Replies are listed 'Best First'.
Re: Timer implementation (time sharing)
by tye (Sage) on Sep 16, 2008 at 02:23 UTC
    real = sum( for all processes, p ){ user(p) + sys(p) };

    Modern computer systems are "time share" systems so the one process you timed isn't the only process getting CPU time. In fact, in the "for all processes", you need to include "the idle 'process'".

    - tye        

      If you sum over all processes, you'll have to divide the result by the number of CPUs :)

      Oh... i think that i understand now. You want to say, that if my application is sleeping on some kind of I/O system calls then Real Time will be Sys+Real+Sleep_Time?
Re: Timer implementation
by GrandFather (Saint) on Sep 16, 2008 at 02:09 UTC

    What is the application? What time resolution do you need? What is the code you have tried?


    Perl reduces RSI - it saves typing
      My application is cgi script. I need at least milli-second. My code is very simple. At case of times(). I use:
      $start = [times()]; ...somecode... $end = [times()]; $time_diff =($start->[0] + $start->[1]) - ($end->[0] + $end->[1]);
      at case of Time::HiRes
      $start=[gettimeofday]; ...somecode... $end = [gettimeofday]; $time_diff = tv_interval($start, $end);
      But if real <=> sys+user, my first case will be always lower that at case 2

        Your implementation context is CGI script, but that is not your application. Your application may be timing a database query or some such. So, what is the application?

        You need to decide what it is that you want to report - wall clock time or process time.

        With most operating systems that are not designed for real time environments reporting wall clock times of less than a few milliseconds is fairly meaningless because time slices for tasks are of that order and will render any real time reporting at that level meaningless.


        Perl reduces RSI - it saves typing
Re: Timer implementation
by betterworld (Curate) on Sep 16, 2008 at 12:32 UTC

    The most obvious example where real isn't the sum of user and sys:

    $ time sleep 3 real 0m3.002s user 0m0.004s sys 0m0.000s

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-25 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found