in reply to Benchmarking regex alternation
Looks to me like the result is real. Adding a few more test cases is interesting:
use strict; use warnings; use Benchmark qw(cmpthese); my $pre = 250; my $post = 250; my $baz_match = ('b' x $pre) . 'baz' . ('b' x $post); my $bar_match = ('b' x $pre) . 'bar' . ('b' x $post); my $no_match = ('b' x $pre) . 'bza' . ('b' x $post); print "using_or_index ", using_or_index (), "\n"; print "using_or_noindex ", using_or_noindex (), "\n"; print "using_or_match ", using_or_match (), "\n"; print "using_or_nomatch ", using_or_nomatch (), "\n"; print "using_alt_match ", using_alt_match (), "\n"; print "using_alt_nomatch ", using_alt_nomatch (), "\n"; print "using_set_match ", using_set_match (), "\n"; print "using_set_nomatch ", using_set_nomatch (), "\n"; cmpthese( -1, { using_or_index => \&using_or_index, using_or_noindex => \&using_or_noindex, using_or_match => \&using_or_match, using_or_nomatch => \&using_or_nomatch, using_alt_match => \&using_alt_match, using_alt_nomatch => \&using_alt_nomatch, using_set_match => \&using_set_match, using_set_nomatch => \&using_set_nomatch, }); sub using_or_index { (-1 != index $baz_match, 'bar') or (-1 != index $baz_match, 'baz') + ? 1 : 0 } sub using_or_noindex { (-1 != index $no_match, 'bar') or (-1 != index $no_match, 'baz') ? + 1 : 0 } sub using_or_match { ($baz_match =~ /bar/ or $baz_match =~ /baz/) ? 1 : 0 } sub using_or_nomatch { ($no_match =~ /bar/ or $no_match =~ /baz/) ? 1 : 0 } sub using_alt_match { ($baz_match =~ /bar|baz/) ? 1 : 0 } sub using_alt_nomatch { ($no_match =~ /bar|baz/) ? 1 : 0 } sub using_set_match { ($baz_match =~ /ba[rz]/) ? 1 : 0 } sub using_set_nomatch { ($no_match =~ /ba[rz]/) ? 1 : 0 }
Prints:
using_or_index 1 using_or_noindex 0 using_or_match 1 using_or_nomatch 0 using_alt_match 1 using_alt_nomatch 0 using_set_match 1 using_set_nomatch 0 Rate using_alt_nomatch using_alt_match using_or_ +nomatch using_or_noindex using_set_nomatch using_or_match using_or_in +dex using_set_match using_alt_nomatch 9692/s -- -22% + -99% -99% -99% -99% - +99% -99% using_alt_match 12385/s 28% -- + -98% -98% -98% -98% - +98% -99% using_or_nomatch 667281/s 6785% 5288% + -- -4% -5% -17% - +19% -26% using_or_noindex 696846/s 7090% 5527% + 4% -- -1% -13% - +16% -22% using_set_nomatch 701884/s 7142% 5567% + 5% 1% -- -13% - +15% -22% using_or_match 804828/s 8204% 6399% + 21% 15% 15% -- +-2% -10% using_or_index 825420/s 8417% 6565% + 24% 18% 18% 3% + -- -8% using_set_match 898573/s 9172% 7155% + 35% 29% 28% 12% + 9% --
My guess is that the regex $str =~ /foo/ devolves to something pretty much like Index $str, 'foo' which can be pretty quick!
The surprise is how fast using a character set with a common prefix match is. That result also goes a long way to validating the benchmark.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Benchmarking regex alternation
by demerphq (Chancellor) on Jan 30, 2007 at 14:37 UTC | |
by sgt (Deacon) on Jan 30, 2007 at 17:27 UTC |