« blib/lib/Benchmark/Perl/Formance/Plugin/Shootout/revcomp.pm
↑ Summary
blib/lib/Benchmark/Perl/Formance/Plugin/DPath.pm »
| 1 | package Benchmark::Perl::Formance::Plugin::RxCmp; | ||||||||||
| 2 | 1 | our $AUTHORITY = 'cpan:SCHWIGON'; | |||||||||
| 3 | # ABSTRACT: benchmark plugin - RxCmp - Compare different Regex engines | ||||||||||
| 4 | |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use 5.010; # [sic - pluggable regex engines] | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | our $VERSION = "0.002"; | |||||||||
| 11 | |||||||||||
| 12 | ############################################################# | ||||||||||
| 13 | # # | ||||||||||
| 14 | # Benchmark Code ahead - Don't touch without strong reason! # | ||||||||||
| 15 | # # | ||||||||||
| 16 | ############################################################# | ||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use Benchmark ':hireswallclock'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 1 | our $goal; | |||||||||
| 22 | 1 | our $count; | |||||||||
| 23 | 1 | our $length; | |||||||||
| 24 | 1 | our $n; | |||||||||
| 25 | 1 | our $re; | |||||||||
| 26 | 1 | our $string; | |||||||||
| 27 | |||||||||||
| 28 | sub native | ||||||||||
| 29 | 0 | ▶ | { | ||||||||
|
Subroutine
native: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 30 | 0 | my ($options) = @_; | |||||||||
| 31 | |||||||||||
| 32 | 0 | my $result; | |||||||||
| 33 | 0 | my $reg = qr/$re/o; | |||||||||
| 34 | 0 | ▶ | my $t = timeit $count, sub { $result = $string =~ $reg }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 35 | return { | ||||||||||
| 36 | 0 | Benchmark => $t, | |||||||||
| 37 | goal => $goal, | ||||||||||
| 38 | count => $count, | ||||||||||
| 39 | result => $result, | ||||||||||
| 40 | # string => $string, | ||||||||||
| 41 | # re => $re, | ||||||||||
| 42 | used_qr_or_precompile => 1, | ||||||||||
| 43 | }; | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub POSIX | ||||||||||
| 47 | 0 | ▶ | { | ||||||||
|
Subroutine
POSIX: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 48 | 0 | my ($options) = @_; | |||||||||
| 49 | |||||||||||
| 50 | 0 | my $reg; | |||||||||
| 51 | ## no critic | ||||||||||
| 52 | 0 | eval ' | |||||||||
| 53 | use POSIX::Regex qw(:all); | ||||||||||
| 54 | $reg = POSIX::Regex->new($re, REG_EXTENDED); | ||||||||||
| 55 | '; | ||||||||||
| 56 | ## use critic | ||||||||||
| 57 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | ▶ | print STDERR "# ".$@ if $options->{verbose} > 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | return { failed => "use failed" }; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | |||||||||||
| 63 | 0 | my $result; | |||||||||
| 64 | 0 | ▶ | my $t = timeit $count, sub { $result = $reg->match($string) }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 65 | return { | ||||||||||
| 66 | 0 | Benchmark => $t, | |||||||||
| 67 | goal => $goal, | ||||||||||
| 68 | count => $count, | ||||||||||
| 69 | result => $result, | ||||||||||
| 70 | # string => $string, | ||||||||||
| 71 | # re => $re, | ||||||||||
| 72 | used_qr_or_precompile => 1, | ||||||||||
| 73 | }; | ||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | sub LPeg | ||||||||||
| 77 | 0 | ▶ | { | ||||||||
|
Subroutine
LPeg: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 78 | 0 | my ($options) = @_; | |||||||||
| 79 | |||||||||||
| 80 | # LPEG regexes seemingly don't work the same way as usual regexes | ||||||||||
| 81 | # therefore the pattern below does not match. | ||||||||||
| 82 | # TODO: Find a equivalent pattern. | ||||||||||
| 83 | 0 | eval "use re::engine::LPEG"; ## no critic | |||||||||
| 84 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | ▶ | print STDERR "# ".$@ if $options->{verbose} > 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | return { failed => "use failed" }; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 0 | return { not_yet_implemented => 'missing comparable equivalent regex' }; | |||||||||
| 90 | |||||||||||
| 91 | 0 | my $result; | |||||||||
| 92 | 0 | my $re_local = ("'a'?" x $n) . ("'a'" x $n); | |||||||||
| 93 | #my $reg = qr/$re_local/; # using that $reg later segfaults | ||||||||||
| 94 | 0 | ▶ | my $t = timeit $count, sub { $result = $string =~ /$re_local/ }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 95 | return { | ||||||||||
| 96 | 0 | Benchmark => $t, | |||||||||
| 97 | goal => $goal, | ||||||||||
| 98 | count => $count, | ||||||||||
| 99 | result => $result, | ||||||||||
| 100 | # string => $string, | ||||||||||
| 101 | # re => $re_local, | ||||||||||
| 102 | used_qr_or_precompile => 0, | ||||||||||
| 103 | }; | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | sub Lua | ||||||||||
| 107 | 0 | ▶ | { | ||||||||
|
Subroutine
Lua: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 108 | 0 | my ($options) = @_; | |||||||||
| 109 | |||||||||||
| 110 | # LPEG regexes seemingly don't work the same way as usual regexes | ||||||||||
| 111 | # therefore the pattern below does not match. | ||||||||||
| 112 | # TODO: Find a equivalent pattern. | ||||||||||
| 113 | # return { not_yet_implemented => 'need to find a equivalent pattern' }; | ||||||||||
| 114 | |||||||||||
| 115 | 0 | eval "use re::engine::Lua"; ## no critic | |||||||||
| 116 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | ▶ | print STDERR "# ".$@ if $options->{verbose} > 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | return { failed => "use failed" }; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 0 | my $result; | |||||||||
| 122 | #my $reg = qr/$re/; # using that $reg later segfaults, unfortunately that makes | ||||||||||
| 123 | 0 | ▶ | my $t = timeit $count, sub { $result = $string =~ /$re/ }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 124 | return { | ||||||||||
| 125 | 0 | Benchmark => $t, | |||||||||
| 126 | goal => $goal, | ||||||||||
| 127 | count => $count, | ||||||||||
| 128 | result => $result, | ||||||||||
| 129 | # string => $string, | ||||||||||
| 130 | # re => $re, | ||||||||||
| 131 | used_qr_or_precompile => 0, | ||||||||||
| 132 | }; | ||||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | sub PCRE | ||||||||||
| 136 | 0 | ▶ | { | ||||||||
|
Subroutine
PCRE: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 137 | 0 | my ($options) = @_; | |||||||||
| 138 | |||||||||||
| 139 | 0 | eval "use re::engine::PCRE"; ## no critic | |||||||||
| 140 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | ▶ | print STDERR "# ".$@ if $options->{verbose} > 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | 0 | return { failed => "use failed" }; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 0 | my $result; | |||||||||
| 146 | 0 | my $reg = qr/$re/o; | |||||||||
| 147 | 0 | ▶ | my $t = timeit $count, sub { $result = $string =~ $reg }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 148 | return { | ||||||||||
| 149 | 0 | Benchmark => $t, | |||||||||
| 150 | goal => $goal, | ||||||||||
| 151 | count => $count, | ||||||||||
| 152 | result => $result, | ||||||||||
| 153 | # string => $string, | ||||||||||
| 154 | # re => $re_local, | ||||||||||
| 155 | used_qr_or_precompile => 1, | ||||||||||
| 156 | }; | ||||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | sub RE2 | ||||||||||
| 160 | 0 | ▶ | { | ||||||||
|
Subroutine
RE2: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 161 | 0 | my ($options) = @_; | |||||||||
| 162 | |||||||||||
| 163 | 0 | eval "use re::engine::RE2"; ## no critic | |||||||||
| 164 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | 0 | ▶ | print STDERR "# ".$@ if $options->{verbose} > 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | 0 | return { failed => "use failed" }; | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 0 | my $result; | |||||||||
| 170 | 0 | my $reg = qr/$re/o; | |||||||||
| 171 | 0 | ▶ | my $t = timeit $count, sub { $result = $string =~ $reg }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 172 | return { | ||||||||||
| 173 | 0 | Benchmark => $t, | |||||||||
| 174 | goal => $goal, | ||||||||||
| 175 | count => $count, | ||||||||||
| 176 | result => $result, | ||||||||||
| 177 | # string => $string, | ||||||||||
| 178 | # re => $re_local, | ||||||||||
| 179 | used_qr_or_precompile => 1, | ||||||||||
| 180 | }; | ||||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | sub Plan9 | ||||||||||
| 184 | 0 | ▶ | { | ||||||||
|
Subroutine
Plan9: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 185 | 0 | my ($options) = @_; | |||||||||
| 186 | |||||||||||
| 187 | 0 | eval "use re::engine::Plan9"; ## no critic | |||||||||
| 188 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | 0 | ▶ | print STDERR "# ".$@ if $options->{verbose} > 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 0 | return { failed => "use failed" }; | |||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | 0 | my $result; | |||||||||
| 194 | 0 | my $reg = qr/$re/o; | |||||||||
| 195 | 0 | ▶ | my $t = timeit $count, sub { $result = $string =~ $reg }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 196 | return { | ||||||||||
| 197 | 0 | Benchmark => $t, | |||||||||
| 198 | goal => $goal, | ||||||||||
| 199 | count => $count, | ||||||||||
| 200 | result => $result, | ||||||||||
| 201 | # string => $string, | ||||||||||
| 202 | # re => $re_local, | ||||||||||
| 203 | used_qr_or_precompile => 1, | ||||||||||
| 204 | }; | ||||||||||
| 205 | } | ||||||||||
| 206 | |||||||||||
| 207 | sub Oniguruma | ||||||||||
| 208 | 0 | ▶ | { | ||||||||
|
Subroutine
Oniguruma: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 209 | 0 | my ($options) = @_; | |||||||||
| 210 | |||||||||||
| 211 | 0 | eval "use re::engine::Oniguruma"; ## no critic | |||||||||
| 212 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 213 | 0 | ▶ | print STDERR "# ".$@ if $options->{verbose} > 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 214 | 0 | return { failed => "use failed" }; | |||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | 0 | my $result; | |||||||||
| 218 | 0 | my $reg = qr/$re/o; | |||||||||
| 219 | 0 | ▶ | my $t = timeit $count, sub { $result = $string =~ $reg }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 220 | return { | ||||||||||
| 221 | 0 | Benchmark => $t, | |||||||||
| 222 | goal => $goal, | ||||||||||
| 223 | count => $count, | ||||||||||
| 224 | result => $result, | ||||||||||
| 225 | # string => $string, | ||||||||||
| 226 | # re => $re, | ||||||||||
| 227 | used_qr_or_precompile => 1, | ||||||||||
| 228 | }; | ||||||||||
| 229 | } | ||||||||||
| 230 | |||||||||||
| 231 | sub regexes | ||||||||||
| 232 | 0 | ▶ | { | ||||||||
|
Subroutine
regexes: not called CC=3 SCAR=18.7
Pod
undocumented
| |||||||||||
| 233 | 0 | my ($options) = @_; | |||||||||
| 234 | |||||||||||
| 235 | # http://swtch.com/~rsc/regexp/regexp1.html | ||||||||||
| 236 | |||||||||||
| 237 | 0 | my %results = (); | |||||||||
| 238 | |||||||||||
| 239 | 1 | ▶ | no strict "refs"; ## no critic | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 240 | 0 | for my $subtest (qw( native POSIX Lua PCRE RE2 Oniguruma Plan9 )) { | |||||||||
| 241 | 0 | ▶ | print STDERR "# - $subtest...\n" if $options->{verbose} > 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 242 | 0 | $results{$subtest} = $subtest->($options); | |||||||||
| 243 | } | ||||||||||
| 244 | # ---------------------------------------------------- | ||||||||||
| 245 | |||||||||||
| 246 | 0 | return \%results; | |||||||||
| 247 | } | ||||||||||
| 248 | |||||||||||
| 249 | sub main | ||||||||||
| 250 | 0 | ▶ | { | ||||||||
|
Subroutine
main: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 251 | 0 | my ($options) = @_; | |||||||||
| 252 | |||||||||||
| 253 | 0 | ▶ | $goal = $options->{fastmode} ? 22 : 29; # probably 28 or more | ||||||||
|
Branch: 1 branch
| |||||||||||
| 254 | 0 | ▶ | $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 255 | 0 | $n = $goal; | |||||||||
| 256 | 0 | $re = ("a?" x $n) . ("a" x $n); | |||||||||
| 257 | 0 | $string = "a" x $n; | |||||||||
| 258 | |||||||||||
| 259 | 0 | return regexes($options); | |||||||||
| 260 | } | ||||||||||
| 261 | |||||||||||
| 262 | 1 | 1; | |||||||||
| 263 | |||||||||||
| 264 | __END__ | ||||||||||