Possible optimization

Leonardo Helman mailscanner at lists.com.ar
Fri Dec 7 13:26:33 GMT 2007


Hi, yesterday, I came across a chunk of code that I never saw before.


--------------------------------------------------------
sub StartTiming {
  my $this = shift;
  my($varprefix, $usertext) = @_;

  my $command = "";
  my $now = time;

  $command = '$this->{' . $varprefix . '_starttime} = $now;';
  eval $command;
}
---------------------------------------------------------


And the same for StopTiming.

I don't understand why you choose an eval there?
It's about 20 times slower than doing something like

$$this{$varprefix}{starttime}=$now;

Or something like this if you prefer

$this->{$varprefix . '_starttime'} = $now;


I'm very curious about it. 
Why??


Here is a benchmark

---------------------------------------------
use Benchmark;
Benchmark::cmpthese(1000000,
                {
                        _eval => '_eval',
                        _hash => '_hash',
                }
        );

sub _eval {
        my $now = time;
        my $cmd = '$pepe{'.$var.'_start} = $now';
        eval $cmd;
}

sub _hash{
        $pepe{$var.'_start'} = time;
}


          Rate _eval _hash
_eval  28843/s    --  -95%
_hash 609756/s 2014%    --

---------------------------------------------

Another one

In the cache functions, the uncompress/compress calls should be better
(cleaner)  inside those functions.


Saludos
Leonardo Helman
PERT Consultores SRL
Argentina




More information about the MailScanner mailing list