How fast is an EVAL String
Stas Bekman
stas at stason.org
Mon Apr 26 15:37:48 CDT 2004
Scott Penrose wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi Dudes.
>
> On my home machine, I have found that a method ('data') in our MI::MDO
> is the largest used piece of code on library (6%) so I had a look at
> it. Basically it takes in a key (eg: DC.Title), uses a regular
> expression to turn '.' into '->' and then uses an eval string to turn
> that into the calls on the methods. I wrote a small test case (next
> bit) which shows a comparison between using an eval and iterating
> through the list...
[...]
> package main;
>
> my $d = Dummy->new();
>
> use Time::HiRes qw( usleep ualarm gettimeofday tv_interval time );
>
> foreach my $m (qw/data1 data2/) {
> my $start = time;
> for my $count (0..9999) {
> foreach my $id (@$list) {
> my $str = $d->$m($id);
> die "Failed data" unless ("" . $str eq "string");
> }
> }
> my $end = time;
> print "Run Time $m = " . ($end - $start) . "\n";
> }
>
>
> Running this I get between 3 and 5 times speed increase using data1 -
> the iterative approach, rather than the eval.
>
> Something to note.
>
> (output of run of 100,000 times - 10 times greater than example above).
>
> $ perl mdo_perf1
> Run Time data1 = 42.5552821159363
> Run Time data2 = 203.071125030518
That's a very unreliable way to run benchmarks, Scott. The results may vary
greatly if the system load varies during the execution. It doesn't change the
results much, but you better use Benchmark for that:
package main;
use strict;
use warnings;
use Benchmark;
my $d = Dummy->new();
timethese(1000,
{
data1 => sub { run("data1") },
data2 => sub { run("data2") },
});
sub run {
my $m = shift;
foreach my $id (@$list) {
my $str = $d->$m($id);
die "Failed data" unless ($str||'' eq "string");
}
}
the methods data1 and data2 are also unfair, the second one sets:
local $SIG{__DIE__} = sub { die $_[0]; };
while the first one does not.
In any case, this doesn't significantly change the point that you were trying
to make :)
__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:stas at stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com
More information about the Melbourne-pm
mailing list