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

Is there any perl code that could be intergrated into a perl program to determine the time and max memory (RAM or RSS) used by the perl program (from the beginning to the end) without adding overhead?

Replies are listed 'Best First'.
Re: time and mem usage of pgm
by BrowserUk (Patriarch) on Feb 05, 2015 at 18:22 UTC

    In Windows, this does the trick provided whole seconds are good enough:

    #! perl -slw use strict; sub mem{ `tasklist /nh /fi "PID eq $$"` =~ m[(\S+ K)$] } END{ print time - $^T, " seconds\nRam:", mem; } sleep $ARGV[0]; __END__ C:\test>junk33 5 5 seconds Ram:4,836 K C:\test>junk33 10 10 seconds Ram:4,832 K

    Finding the *nix equivalent of tasklist left as an exercise.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      how is it possible to test in perl if the program is running on win or linux? In this way, the same program can be used on both system and if it is under linux, proc/stat will be used to measure time and mem and if it is win, your code.

        how is it possible to test in perl if the program is running on win or linux?

        Simply:

        if( $^O eq 'MSWin32' ) { ## Do the windows thing } else { ## else do something else }

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

        You don't need to test, if there is a tasklist.exe pslist.exe of memlist.exe, it doesn't matter what platform it is, their usage is probably the same on win32 and on linux :)

        Devel::CheckOS, Devel::AssertOS

Re: time and mem usage of pgm
by Anonymous Monk on Feb 05, 2015 at 21:00 UTC

    On linux, one might use the following:

    sub mem{ `ps -o rss $$` =~ m[(\d+)] }
    But directly reading the process stats is preferable:
    sub mem { local $/; open my $fh, '/proc/self/status'; <$fh> =~ /^VmRSS:\s*(\d+)/m; }

      yes, it is under linux that I want to measure.

      How about the time that the whole program takes (sytem, user, wall clock)?

        How about the time that the whole program takes (sytem, user, wall clock)?

        See times for 2 out of 3.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: time and mem usage of pgm
by Anonymous Monk on Feb 05, 2015 at 23:10 UTC