http://www.perlmonks.org?node_id=928742


in reply to regex alternation

Hooray, make of this what you will, at your own peril

And see also: Why does global match run faster than none global?, Multiple Regex evaluations or one big one?

#!/usr/bin/perl -- use strict; use warnings; use Benchmark qw[ cmpthese ]; print "##########\n"; print "perl $] \n"; my @test_array = ( "in directory '/directory/tmp'", "in directory '/directory/tmp/.idBAA5KaaYWfb'", "in directory '/directory/not'", ( "in directory '/directory/notit'" ) x 2, ); cmpthese -3, { a_ => sub { my $match = 0; my $test3 = qr<^in directory '/directory/tmp(/\..*'|')$>; for (@test_array) { $match++ if $_ =~ $test3; } }, a => sub { my $match = 0; my $test3 = qr<^in directory '/directory/tmp(/\..*'|')$>; for my $line (@test_array) { $match++ if $line =~ $test3; } }, b => sub { my $match = 0; my $test3 = qr<^in directory '/directory/tmp(?:/\..*'|')$>; for my $line (@test_array) { $match++ if $line =~ $test3; } }, c => sub { my $match = 0; my $test3 = qr<^in directory '/directory/tmp(?:'|/\..*')$>; for my $line (@test_array) { $match++ if $line =~ $test3; } }, f => sub { my $match = 0; my $test3 = qr<^in directory '/directory/tmp(?:/\..*)?'$>; for my $line (@test_array) { $match++ if $line =~ $test3; } }, g => sub { my $match = 0; my $prefix = q<in directory '/directory/tmp>; my $prefix_len = length $prefix; my $test3 = qr<^$prefix(/\..*'|')$>; for my $line (@test_array) { $match++ if substr($line, 0, $prefix_len) eq $prefix and $line =~ $test3 } }, d => sub { my $match = 0; my $test3 = qr<^in directory '/directory/tmp'$>; my $test4 = qr<^in directory '/directory/tmp/\..*'$>; for my $line (@test_array) { $match++ if $line =~ $test3 or $line =~ $test4; } }, e => sub { my $match = 0; my $test3 = q<in directory '/directory/tmp'>; my $test4 = qr<^in directory '/directory/tmp/\..*'$>; for my $line (@test_array) { $match++ if $line eq $test3 or $line =~ $test4; } }, }; __END__ ########## perl 5.006001 Benchmark: running a, a_, b, c, d, e, f, g, each for at least 3 CPU se +conds... a: 3 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @ 19 +0674.36/s (n=577934) a_: 4 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @ 19 +0674.36/s (n=577934) b: 4 wallclock secs ( 3.22 usr + 0.00 sys = 3.22 CPU) @ 20 +6861.45/s (n=665887) c: 3 wallclock secs ( 3.06 usr + 0.00 sys = 3.06 CPU) @ 21 +3192.95/s (n=653010) d: 3 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 15 +6451.74/s (n=486252) e: 4 wallclock secs ( 3.25 usr + 0.00 sys = 3.25 CPU) @ 21 +6385.04/s (n=703035) f: 4 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 19 +4816.72/s (n=605880) g: 3 wallclock secs ( 3.22 usr + 0.00 sys = 3.22 CPU) @ 14 +8028.89/s (n=476505) Rate g d a_ a f b c e g 148029/s -- -5% -22% -22% -24% -28% -31% -32% d 156452/s 6% -- -18% -18% -20% -24% -27% -28% a_ 190674/s 29% 22% -- -0% -2% -8% -11% -12% a 190674/s 29% 22% 0% -- -2% -8% -11% -12% f 194817/s 32% 25% 2% 2% -- -6% -9% -10% b 206861/s 40% 32% 8% 8% 6% -- -3% -4% c 213193/s 44% 36% 12% 12% 9% 3% -- -1% e 216385/s 46% 38% 13% 13% 11% 5% 1% -- ########## perl 5.014001 Rate d a_ a f b c g e d 49781/s -- -31% -31% -34% -35% -38% -38% -46% a_ 71676/s 44% -- -1% -4% -6% -10% -11% -22% a 72090/s 45% 1% -- -4% -6% -10% -10% -21% f 75019/s 51% 5% 4% -- -2% -6% -6% -18% b 76633/s 54% 7% 6% 2% -- -4% -4% -16% c 79672/s 60% 11% 11% 6% 4% -- -1% -13% g 80135/s 61% 12% 11% 7% 5% 1% -- -13% e 91585/s 84% 28% 27% 22% 20% 15% 14% --