Perl equivalent of unix time command

Discussion in 'Perl Misc' started by Melroy, Sep 7, 2008.

  1. Melroy

    Melroy Guest

    Hi all,
    I want to benchmark a bunch of running processes using Perl , but I
    could not find any equivalent for the unix time coammnd in perl? Can
    someone point to me
    if such a function exists in perl? The only command I found was
    "times",
    but I don't know how to use it. doing a websearch did not help. If the
    function
    "times" does teh job, can someone show me an example of how to use it?

    Thanks
    Melroy, Sep 7, 2008
    #1
    1. Advertising

  2. Melroy <> wrote:
    >I want to benchmark a bunch of running processes using Perl ,


    You may want to check 'perldoc -q profile':
    How do I profile my Perl programs?

    > but I
    >could not find any equivalent for the unix time coammnd in perl? Can
    >someone point to me
    >if such a function exists in perl?


    There can't be an exact equivalent, because Unix time reports the total
    time _AFTER_ the process has already terminated while in Perl the script
    would still be running when you are executing the time check. So at best
    it can report the elapsed time of the script until now.

    >The only command I found was
    >"times",
    >but I don't know how to use it.


    You may want to check the documentation: perldoc -f times

    jue
    Jürgen Exner, Sep 7, 2008
    #2
    1. Advertising

  3. Melroy

    Melroy Guest

    Hi,
    I want to profile a bunch of command line programs using perl. I tried
    to call the unix
    time comamnd using backslash as well as the system comamnd but that
    does not seem to capture the output. So i thought to check if there is
    something native to perl.
    Thanks

    > You may want to check 'perldoc -q profile':
    >           How do I profile my Perl programs?
    >
    > > but I
    > >could not find any equivalent for the unix time coammnd in perl? Can
    > >someone point to me
    > >if such a function exists in perl?

    >
    > There can't be an exact equivalent, because Unix time reports the total
    > time _AFTER_ the process has already terminated while in Perl the script
    > would still be running when you are executing the time check. So at best
    > it can report the elapsed time of the script until now.
    >
    > >The only command I found was
    > >"times",
    > >but I don't know how to use it.

    >
    > You may want to check the documentation: perldoc -f times
    >
    > jue
    Melroy, Sep 7, 2008
    #3
  4. Melroy

    Melroy Guest

    More speccifically here is what I tried following
    http://bytes.com/forum/thread678672.html
    but no use :-(

    my $v = `time -p gzip energylossmc.dat 1>/dev/null 2>&1` ;
    print $v ;
    Nothing seems to get captured in the variable v?
    What am I doing wrong?
    I also replaced time with /usr/bin/time
    but no use.

    Any help is greatly appreciated
    Melroy, Sep 7, 2008
    #4
  5. >>>>> "Melroy" == Melroy <> writes:

    Melroy> my $v = `time -p gzip energylossmc.dat 1>/dev/null 2>&1` ;
    Melroy> print $v ;

    Yes. You've just said "make stdout go to dev null instead of being
    copied to the variable, and while you're at it, make stderr also go
    there too".

    Why would you expect anything to be in $v after this?

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
    Randal L. Schwartz, Sep 8, 2008
    #5
  6. Melroy <> writes:

    > my $v = `time -p gzip energylossmc.dat 1>/dev/null 2>&1` ;
    > print $v ;
    > Nothing seems to get captured in the variable v?


    Your shell command redirects both stdout and stderr to /dev/null, so
    there's nothing to capture.

    sherm--

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Sherm Pendley, Sep 8, 2008
    #6
  7. [Please do not top-post. It interrupts the normal flow of reading and
    leaves people wondering which part of a message your comments are
    referring to.]

    Melroy <> wrote:
    >> You may want to check 'perldoc -q profile':
    >>           How do I profile my Perl programs?


    >I want to profile a bunch of command line programs using perl. I tried
    >to call the unix


    Sorry, then I misunderstood your original request.

    >time comamnd using backslash as well as the system comamnd but that
    >does not seem to capture the output. So i thought to check if there is
    >something native to perl.



    Backslash? Could you post an actual code sample?

    As for system(): it is not supposed to capture any output, please see
    third paragraph, third sentence of the documentation of system().

    jue
    Jürgen Exner, Sep 8, 2008
    #7
  8. Melroy <> wrote:
    >my $v = `time -p gzip energylossmc.dat 1>/dev/null 2>&1` ;
    >print $v ;
    >Nothing seems to get captured in the variable v?
    >What am I doing wrong?


    My shell skills are somewhat rusty, but I think you are redirecting
    stdin as well as stdout to /dev/null.
    And aside of those two there simply is nothing else that could possibly
    be outputted by the command and captured in $v.

    jue
    Jürgen Exner, Sep 8, 2008
    #8
  9. Jürgen Exner <> wrote:
    >Melroy <> wrote:
    >>my $v = `time -p gzip energylossmc.dat 1>/dev/null 2>&1` ;
    >>print $v ;
    >>Nothing seems to get captured in the variable v?
    >>What am I doing wrong?

    >
    >My shell skills are somewhat rusty, but I think you are redirecting
    >stdin as well as stdout to /dev/null.


    Daaaah! Make that STDOUT and STDERR, of course.

    >And aside of those two there simply is nothing else that could possibly
    >be outputted by the command and captured in $v.
    >
    >jue
    Jürgen Exner, Sep 8, 2008
    #9
  10. Melroy

    Guest

    Melroy <> wrote:
    > Hi all,
    > I want to benchmark a bunch of running processes using Perl ,


    Why?

    > but I
    > could not find any equivalent for the unix time coammnd in perl?


    system "time $process @args" ....;


    Can
    > someone point to me
    > if such a function exists in perl? The only command I found was
    > "times",
    > but I don't know how to use it.


    Capture the value before the external program is run, capture again
    after, and subtract.


    > doing a websearch did not help. If the
    > function
    > "times" does teh job, can someone show me an example of how to use it?


    $ perl -l
    my @x= times;
    system q{perl -le '1 foreach (1..1e7)'};
    my $i;
    foreach( times) {
    print $_ - $x[$i++]
    }
    __END__
    0
    0
    0.57
    0

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Sep 8, 2008
    #10
  11. Melroy

    Guest

    Melroy <> wrote:
    > More speccifically here is what I tried following
    > http://bytes.com/forum/thread678672.html
    > but no use :-(
    >
    > my $v = `time -p gzip energylossmc.dat 1>/dev/null 2>&1` ;
    > print $v ;
    > Nothing seems to get captured in the variable v?


    You sent the gzip output to /dev/null, so there is nothing on stdout to
    capture.

    On my machine, the redirection only applies to gzip, not to time, so
    time's output goes where it otherwise would, which is stderr, which is
    not captured by backticks. So it just goes to my console.


    > What am I doing wrong?
    > I also replaced time with /usr/bin/time
    > but no use.
    >
    > Any help is greatly appreciated


    my $x = `time -p (gzip energylossmc.dat 1>/dev/null 2>&1) 2>&1 `

    The parenthesis forces the last 2>&1 to apply to time, sending its stderr
    to stdout, where it is captured.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Sep 8, 2008
    #11
  12. Melroy

    Melroy Guest

    Hi,
    Thanks a lot for all the replies. One more question I have is whether
    there is a module
    written in perl to track the max. memory usage of a launched job(such
    as the one in this
    thread). The unix "time" command does not seem to help.
    Thanks
    Melroy, Sep 8, 2008
    #12
  13. Melroy <> writes:

    > Hi,
    > Thanks a lot for all the replies. One more question I have is whether
    > there is a module
    > written in perl to track the max. memory usage of a launched job(such
    > as the one in this
    > thread). The unix "time" command does not seem to help.
    > Thanks


    Try Unix::Getrusage. However, on some Unices (eg. Linux when I last
    tried) the memory fields aren't set, so you'll have to do it some other
    way -- for example, examining /proc/<pid>/status periodically, or
    enabling BSD process accounting and parsing data from `sa'...

    --
    T.
    Tomislav Novak, Sep 9, 2008
    #13
  14. Melroy

    Guest

    Melroy <> wrote:
    > Hi,
    > Thanks a lot for all the replies. One more question I have is whether
    > there is a module
    > written in perl to track the max. memory usage of a launched job(such
    > as the one in this
    > thread). The unix "time" command does not seem to help.


    The time command is supposed to be able to do that. Since that feature
    is not implemented on your system, I would guess it is because your
    OS doesn't provide good tools for doing so. So you probably have a
    challenge on your hands. You could capture the pid of the spawned process,
    then check the size of that process periodically (using ps, or something
    else), taking the max of the observed sizes. It would always be possible
    that the process requests large amounts of memory just before exiting,
    which you would miss if your periodic inspection didn't get lucky.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Sep 9, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pekka niiranen
    Replies:
    5
    Views:
    698
    Tim G
    Mar 3, 2005
  2. W3
    Replies:
    2
    Views:
    730
    c james
    Jul 23, 2007
  3. Rob Lucas
    Replies:
    9
    Views:
    143
    Thufir
    Dec 9, 2007
  4. Replies:
    4
    Views:
    402
    John W. Krahn
    Jan 31, 2005
  5. Mike

    Perl equivalent to unix script

    Mike, Oct 30, 2006, in forum: Perl Misc
    Replies:
    11
    Views:
    393
    Peter J. Holzer
    Nov 3, 2006
Loading...

Share This Page