in reply to Re: Hash/Array of Regular Expressions?
in thread Hash/Array of Regular Expressions?
$ perl multire.pl
Benchmark: running using alternates, using foreach, each for at least 30 CPU seconds...
using alternates: 30 wallclock secs (30.07 usr + 0.09 sys = 30.16 CPU) @ 2.69/s (n=81)
using foreach: 30 wallclock secs (30.09 usr + 0.02 sys = 30.11 CPU) @ 0.20/s (n=6)
s/iter using foreach using alternates
using foreach 5.02 -- -93%
using alternates 0.372 1248% --
Here's the code:
#!/usr/bin/perl -w use strict; use Benchmark; #$ wc /usr/share/dict/words # 45424 45424 409276 /usr/share/dict/words my $file = '/usr/share/dict/words'; # These callbacks could do whatever you want. # They have the matching line passed in. sub foundThe { } sub foundAeig { } sub foundCat { } my %specs = ( '^the' => \&foundThe, 'at[ei]g' => \&foundAeig, 'cat$' => \&foundCat, ); my %cookedSpecs = map { qr{$_}, $specs{$_} } keys(%specs); sub usingForeach { $count = 0; open FILE, $file or die "can't open $file: $!\n"; while (<FILE>) { foreach my $pattern ( keys(%cookedSpecs) ) { if (m{$pattern}) { $cookedSpecs{$pattern}->($_); } } } close FILE; } sub usingAlternates { my $altpattern = join ( '|', map {qr{$_}} keys(%specs) ); $count = 0; open FILE, $file or die "can't open $file: $!\n"; while (<FILE>) { if (m{$altpattern}) { foreach my $pattern ( keys(%cookedSpecs) ) { if (m{$pattern}) { $cookedSpecs{$pattern}->($_); } } } } close FILE; } Benchmark::cmpthese( -30, { 'using foreach' => \&usingForeach, 'using alternates' => \&usingAlternates } );
update: clarified callbacks: they don't have to do the same thing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Hash/Array of Regular Expressions?
by Aighearach (Initiate) on Jun 25, 2001 at 01:01 UTC | |
by bikeNomad (Priest) on Jun 25, 2001 at 01:50 UTC | |
by Aighearach (Initiate) on Jun 25, 2001 at 02:05 UTC |