in reply to Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???
Using a /(foo|bar)/ regex on strings is slower than using a foreach loop doing the matching one after another.Not in my benchmark:
use strict; use warnings; use Benchmark 'cmpthese'; undef $/; my $str = <DATA>; cmpthese(-2, { alternation => sub { my @m = $str =~ /(foo|bar)/g }, map => sub { my @m = map $str =~ /$_/g, qw(foo bar) }, loop => sub { my @m; push @m, $str =~ /$_/g for qw(foo bar) } +, }); cmpthese(-2, { alt_s => sub { my $s2 = $str; $s2 =~ s/(foo|bar)//g }, loop_s => sub { my $s2 = $str; $s2 =~ s/$_//g for qw(foo bar) } }); __DATA__ This string contains foo and bar for fools and bards and I pity the foo who bars the way
Update:Rate map loop alternation map 14977/s -- -6% -25% loop 15986/s 7% -- -20% alternation 20090/s 34% 26% -- Rate loop_s alt_s loop_s 17436/s -- -37% alt_s 27643/s 59% --
Results:use strict; use Benchmark 'cmpthese'; foreach my $regexcount (10) { foreach my $regexlength (5,20) { my @items = map{ createRandomTextWithLength($regexlength) } + (1..$regexcount); my $regexstr = join('|',@items); my $regex = qr/(?:$regexstr)/; foreach my $stringlength (1000,100000) { print join "\n", "Stringlength: $stringlength", "Number of Regexes:$regexcount", "Length of each Regex +:$regexlength\n"; my $teststring = createRandomTextWithLength($stringlength) +; cmpthese(-2, { alt => sub { my $test=$teststring; $test =~ s/$regex/foobar/g; }, for => sub { my $test=$teststring; foreach my $oneregex (@items) { $test =~ s/$oneregex/foobar/g; } } }); } } } sub createRandomTextWithLength($) { my($count) = (@_); my $string; for (1.. $count) { $string.=chr(ord('a')+rand(20)) } return $string; }
Stringlength: 1000 Number of Regexes:10 Length of each Regex:5 Rate alt for alt 1296/s -- -70% for 4357/s 236% -- Stringlength: 100000 Number of Regexes:10 Length of each Regex:5 Rate alt for alt 12.7/s -- -97% for 379/s 2877% -- Stringlength: 1000 Number of Regexes:10 Length of each Regex:20 Rate alt for alt 1324/s -- -68% for 4144/s 213% -- Stringlength: 100000 Number of Regexes:10 Length of each Regex:20 Rate alt for alt 12.7/s -- -98% for 676/s 5209% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???
by hardburn (Abbot) on Feb 18, 2005 at 16:03 UTC | |
by kscaldef (Pilgrim) on Feb 19, 2005 at 05:45 UTC |