[Pdx-pm] oh, gross (object method in regex)

Randall Hansen randall at sonofhans.net
Sun Mar 12 16:29:50 PST 2006


On Mar 12, 2006, at 4:20 PM, Randall Hansen wrote:

oh, der, typo in the benchmark.  secret magic way is still slow, but  
not as bad as my previous email implied.

#!/usr/bin/perl
use strict;
use warnings;

package Foo;
     sub new { return bless {}, shift }
     sub foo { 'foo' }

package main;
     use Data::Dumper;
     use Benchmark qw/ :all /;

     my $count = 100_000;

     timethese( $count, {
         'temp'    => \&temp,
         'deref'   => \&deref,
         'eric'    => \&eric,
         'eric_correct'   => \&eric_correct,
     });

     sub temp {
         my $Foo = Foo->new;
         my @search = qw/ baz bar foo bang /;
         return grep /${ \$Foo->foo }/ => @search;
     }

     sub deref {
         my $Foo = Foo->new;
         my @search = qw/ baz bar foo bang /;
         my $foo = $Foo->foo;
         return grep /$foo/ => @search;
     }

     sub eric {
         my $Foo = Foo->new;
         my @search = qw/ baz bar foo bang /;
         return grep /@{[ \$Foo->foo ]}/ => @search;
     }

     sub eric_correct {
         my $Foo = Foo->new;
         my @search = qw/ baz bar foo bang /;
         return grep /@{[ $Foo->foo ]}/ => @search;
     }

__END__
results:
Benchmark: timing 100000 iterations of deref, eric, eric_correct,  
temp...
        deref:  2 wallclock secs ( 1.45 usr +  0.01 sys =  1.46 CPU)  
@ 68493.15/s (n=100000)
         eric: 13 wallclock secs ( 9.82 usr +  0.12 sys =  9.94 CPU)  
@ 10060.36/s (n=100000)
eric_correct:  4 wallclock secs ( 2.92 usr +  0.03 sys =  2.95 CPU) @  
33898.31/s (n=100000)
         temp:  2 wallclock secs ( 2.15 usr +  0.03 sys =  2.18 CPU)  
@ 45871.56/s (n=100000)




More information about the Pdx-pm-list mailing list