Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Your favorite objects NOT of the hashref phylum

by codeacrobat (Chaplain)
on Mar 23, 2006 at 15:57 UTC ( [id://538772]=note: print w/replies, xml ) Need Help??


in reply to Your favorite objects NOT of the hashref phylum

A stopwatch for example.
package Stopwatch; sub new{bless \$_[1], $_[0]} sub diff{`date +%s` - ${$_[0]}} $a=new Stopwatch(`date +%s`); sleep rand()*10; print "I slept for ", $a->diff, " seconds\n";

Replies are listed 'Best First'.
Re^2: Your favorite objects NOT of the hashref phylum
by xdg (Monsignor) on Mar 23, 2006 at 16:14 UTC

    How about just using the builtin Perl function time instead of backticks and the unix date function?

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Thanx, sometimes I forget the simplest things. I just needed a quick example illustrating the Stopwatch. I guess the reason I forgot about time is that there are so many similar functions (time, times, gmtime, localtime, asctime, mktime, andwhatnottime).
      It's good to have the backticks if you got lost but know a non-perl alternative.
        Thanx, sometimes I forget the simplest things

        Me, too. Sometimes, I find there's nothing for it but a good skim through perlfunc, which has a nicely categorized list of all the built-in functions (and has all the long descriptions that perldoc -f gives you).

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      I remember using date in a log function once as a Perl junior. I finally noticed it because my code was taking a while to run, so I did my first ever profiling run on it. The backtick date was consuming 60% of the CPU time for the entire process.
        It looks like it is more than 60% (cygwin, samsung x25). It does not matter whether you use backticks or a regular open to the date program, both are equally slow.
        $ time perl -e 'do {open PROGRAM, "date +%s|" or die "can not open dat +e $!";$time = <PROGRAM>;close PROGRAM} for 0 .. 1000' real 0m14.733s user 0m32.927s sys 0m9.109s $ time perl -e 'do { $time = `date +%s`} for 0 .. 1000' real 0m14.717s user 0m32.929s sys 0m9.138s $ time perl -e 'do { $time = time} for 0 .. 1000' real 0m0.084s user 0m0.061s sys 0m0.046s

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found