Perl equivalent of unix time command

This is a discussion on Perl equivalent of unix time command within the Perl forums in Programming Languages category; 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...

Go Back   Application Development Forum > Programming Languages > Perl

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-07-2008, 12:33 PM
Melroy
Guest
 
Default Perl equivalent of unix time command

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
Reply With Quote
  #2  
Old 09-07-2008, 12:53 PM
Jürgen Exner
Guest
 
Default Re: Perl equivalent of unix time command

Melroy <melroysoares@hotmail.com> 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
Reply With Quote
  #3  
Old 09-07-2008, 01:37 PM
Melroy
Guest
 
Default Re: Perl equivalent of unix time command

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


Reply With Quote
  #4  
Old 09-07-2008, 05:29 PM
Melroy
Guest
 
Default Re: Perl equivalent of unix time command

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
Reply With Quote
  #5  
Old 09-07-2008, 07:08 PM
Randal L. Schwartz
Guest
 
Default Re: Perl equivalent of unix time command

>>>>> "Melroy" == Melroy <melroysoares@hotmail.com> 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
<merlyn@stonehenge.com> <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
Reply With Quote
  #6  
Old 09-07-2008, 09:47 PM
Sherm Pendley
Guest
 
Default Re: Perl equivalent of unix time command

Melroy <melroysoares@hotmail.com> 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
Reply With Quote
  #7  
Old 09-07-2008, 11:29 PM
Jürgen Exner
Guest
 
Default Re: Perl equivalent of unix time command

[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 <melroysoares@hotmail.com> 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
Reply With Quote
  #8  
Old 09-07-2008, 11:32 PM
Jürgen Exner
Guest
 
Default Re: Perl equivalent of unix time command

Melroy <melroysoares@hotmail.com> 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
Reply With Quote
  #9  
Old 09-07-2008, 11:39 PM
Jürgen Exner
Guest
 
Default Re: Perl equivalent of unix time command

Jürgen Exner <jurgenex@hotmail.com> wrote:
>Melroy <melroysoares@hotmail.com> 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

Reply With Quote
  #10  
Old 09-08-2008, 12:28 PM
xhoster@gmail.com
Guest
 
Default Re: Perl equivalent of unix time command

Melroy <melroysoares@hotmail.com> 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.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 09:54 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.