[DGBI] oops, obviously I've missed something (was: regexp or array?)
- From: Eric Pozharski <whynot@xxxxxxxxxxxxxx>
- Date: Thu, 19 Feb 2009 05:08:53 +0200
On 2009-02-18, Peter Makholm <peter@xxxxxxxxxxx> wrote:
End of Road <joblo@xxxxxxxxx> writes:
Using regular expressions
$strs = 'str1|str2|str3";
if ($str =~ /strs/)
...
Or using an array
@strs = qw/str1,str2, str3/;
foreach (@strs) {
if ($_[0] =~ /$_/)
...
Better use an array of precompiled regexpes:
my @regexp = map { qr/$_/ } qw(foo bar baz);
if( grep { $_[0] =~ $_ } @regexp ) {
...
}
perl -wle '
use Benchmark qw|cmpthese timethese|;
my @arg = qw{ aa cc ee ff };
my $x = qr{a|b|c};
my $y = q{a|b|c};
cmpthese timethese -10, {
pure => sub { m{a|b|c} foreach @arg },
prec => sub { m{$x} foreach @arg },
expl => sub { $_ =~ $x foreach @arg },
alt => sub { $x foreach @arg },
eval => sub { m{$y} foreach @arg },
};
'
Useless use of private variable in void context at -e line 10.
Benchmark:
running
alt, eval, expl, prec, pure
for at least 10 CPU seconds
....
alt: 12 wallclock secs (10.38 usr + 0.02 sys = 10.40 CPU) @ 267620.77/s (n=2783256)
eval: 11 wallclock secs (10.10 usr + 0.04 sys = 10.14 CPU) @ 57220.81/s (n=580219)
expl: 11 wallclock secs (10.52 usr + 0.04 sys = 10.56 CPU) @ 37743.47/s (n=398571)
prec: 11 wallclock secs (10.56 usr + 0.02 sys = 10.58 CPU) @ 38041.49/s (n=402479)
pure: 11 wallclock secs (10.36 usr + 0.02 sys = 10.38 CPU) @ 61972.35/s (n=643273)
Rate expl prec eval pure alt
expl 37743/s -- -1% -34% -39% -86%
prec 38041/s 1% -- -34% -39% -86%
eval 57221/s 52% 50% -- -8% -79%
pure 61972/s 64% 63% 8% -- -77%
alt 267621/s 609% 603% 368% 332% --
I've checked 4 times, C<expl> and C<prec> flip-flops, while
C<eval> and C<pure> are stable. What I've missed this time?
C<alt> seems to be a *way* fast, right? Left as an excersise for curious
readers. (spoiler, since rot13 is letter only, 100 empty lines below)
perl -wle '
$x = qr{(?{ print 1 })};
@y = qr{ a b c };
$x foreach (@y)'
Useless use of a variable in void context at -e line 4.
*CUT*
--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
.
- Follow-Ups:
- References:
- regexp or array?
- From: End of Road
- Re: regexp or array?
- From: Peter Makholm
- regexp or array?
- Prev by Date: FAQ 7.11 How do I create a class?
- Next by Date: Re: Net::SSH2 scp_put not working!
- Previous by thread: Re: regexp or array?
- Next by thread: Re: [DGBI] oops, obviously I've missed something (was: regexp or array?)
- Index(es):
Relevant Pages
|