Perl equivalent of unix time command

M

Melroy

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
 
J

Jürgen Exner

Melroy said:
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
 
M

Melroy

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
 
M

Melroy

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
 
R

Randal L. Schwartz

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?
 
S

Sherm Pendley

Melroy said:
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--
 
J

Jürgen Exner

[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.]

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

Jürgen Exner

Melroy said:
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

Jürgen Exner

Jürgen Exner said:
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.
 
X

xhoster

Melroy said:
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.
 
X

xhoster

Melroy said:
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.
 
M

Melroy

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
 
T

Tomislav Novak

Melroy said:
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'...
 
X

xhoster

Melroy said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top