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