| 1 | package Benchmark::Perl::Formance::Plugin::PerlStone2015::regex; | ||||||||||
| 2 | 1 | our $AUTHORITY = 'cpan:SCHWIGON'; | |||||||||
| 3 | # ABSTRACT: benchmark - regex - regular expression handling | ||||||||||
| 4 | 1 | $Benchmark::Perl::Formance::Plugin::PerlStone2015::regex::VERSION = '0.002'; | |||||||||
| 5 | # Regexes | ||||||||||
| 6 | |||||||||||
| 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 | ############################################################# | ||||||||||
| 11 | # # | ||||||||||
| 12 | # Benchmark Code ahead - Don't touch without strong reason! # | ||||||||||
| 13 | # # | ||||||||||
| 14 | ############################################################# | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use Benchmark ':hireswallclock'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | sub backtrack | ||||||||||
| 20 | 0 | ▶ | { | ||||||||
|
Subroutine
backtrack: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 21 | 0 | my ($options) = @_; | |||||||||
| 22 | |||||||||||
| 23 | # http://swtch.com/~rsc/regexp/regexp1.html | ||||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | my $goal = $options->{fastmode} ? 25 : 28; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | 0 | my $n = $goal; | |||||||||
| 29 | 0 | my $re = ("a?" x $n) . ("a" x $n); | |||||||||
| 30 | 0 | my $string = "a" x $n; | |||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | my $t = timeit $count, sub { $string =~ /$re/ }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 33 | return { | ||||||||||
| 34 | 0 | Benchmark => $t, | |||||||||
| 35 | goal => $goal, | ||||||||||
| 36 | count => $count, | ||||||||||
| 37 | }; | ||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | sub split1 | ||||||||||
| 41 | 0 | ▶ | { | ||||||||
|
Subroutine
split1: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 42 | 0 | my ($options) = @_; | |||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | my $goal = $options->{fastmode} ? 5_000_000 : 50_000_000; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | 0 | my $re = '(.*) (.*) (.*) (.*) (.*)'; | |||||||||
| 48 | 0 | my $string = (("a" x $goal) . " ") x 5; | |||||||||
| 49 | 0 | chop $string; | |||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | my $t = timeit $count, sub { $string =~ /$re/ }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 52 | return { | ||||||||||
| 53 | 0 | Benchmark => $t, | |||||||||
| 54 | goal => $goal, | ||||||||||
| 55 | count => $count, | ||||||||||
| 56 | }; | ||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | sub split2 | ||||||||||
| 60 | 0 | ▶ | { | ||||||||
|
Subroutine
split2: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 61 | 0 | my ($options) = @_; | |||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | my $goal = $options->{fastmode} ? 25_000_000 : 250_000_000; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 0 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | 0 | my $re = '([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*)'; | |||||||||
| 67 | 0 | my $string = ( ("a" x $goal) . " " ) x 5; | |||||||||
| 68 | 0 | chop $string; | |||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | my $t = timeit $count, sub { $string =~ /$re/ }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 71 | return { | ||||||||||
| 72 | 0 | Benchmark => $t, | |||||||||
| 73 | goal => $goal, | ||||||||||
| 74 | count => $count, | ||||||||||
| 75 | }; | ||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub precomp_access | ||||||||||
| 79 | 0 | ▶ | { | ||||||||
|
Subroutine
precomp_access: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 80 | 0 | my ($options) = @_; | |||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | my $goal = $options->{fastmode} ? 50 : 500; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 0 | my $r = qr/\d+/; | |||||||||
| 86 | 0 | ▶ | my $t = timeit $count, sub { "1234" =~ $r for 1..50_000*$goal }; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 87 | return { | ||||||||||
| 88 | 0 | Benchmark => $t, | |||||||||
| 89 | goal => $goal, | ||||||||||
| 90 | count => $count, | ||||||||||
| 91 | }; | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # how quickly run-time regexes are compiled | ||||||||||
| 95 | sub runtime_comp | ||||||||||
| 96 | 0 | ▶ | { | ||||||||
|
Subroutine
runtime_comp: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 97 | 0 | my ($options) = @_; | |||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | my $goal = $options->{fastmode} ? 5_000_000 : 10_000_000; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | |||||||||||
| 102 | 0 | my $r ='\d+'; | |||||||||
| 103 | 0 | ▶ | my $t = timeit $count, sub { "1234" =~ $r for 1..$goal }; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 104 | return { | ||||||||||
| 105 | 0 | Benchmark => $t, | |||||||||
| 106 | goal => $goal, | ||||||||||
| 107 | count => $count, | ||||||||||
| 108 | }; | ||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | # run-time regexes are compiled but defeating the caching | ||||||||||
| 112 | sub runtime_comp_nocache | ||||||||||
| 113 | 0 | ▶ | { | ||||||||
|
Subroutine
runtime_comp_nocache: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 114 | 0 | my ($options) = @_; | |||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | my $goal = $options->{fastmode} ? 5_000_000 : 10_000_000; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | |||||||||||
| 119 | 0 | my $r ='\d+'; | |||||||||
| 120 | 0 | ▶ | my $t = timeit $count, sub { "1234" =~ /$r$_/ for 1..$goal }; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 121 | return { | ||||||||||
| 122 | 0 | Benchmark => $t, | |||||||||
| 123 | goal => $goal, | ||||||||||
| 124 | count => $count, | ||||||||||
| 125 | }; | ||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | # run-time code-blocks | ||||||||||
| 129 | sub code_runtime | ||||||||||
| 130 | 0 | ▶ | { | ||||||||
|
Subroutine
code_runtime: not called CC=3 SCAR=20.6
Pod
undocumented
| |||||||||||
| 131 | 0 | my ($options) = @_; | |||||||||
| 132 | |||||||||||
| 133 | 0 | ▶ | my $goal = $options->{fastmode} ? 50_000 : 200_000; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | |||||||||||
| 136 | 0 | my $counter; | |||||||||
| 137 | 0 | my $code = '(?{$counter++})'; | |||||||||
| 138 | 1 | ▶ | use re 'eval'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 139 | |||||||||||
| 140 | 0 | ▶ | my $t = timeit $count, sub { $counter = 0; "1234" =~ /\d+$code/ for 1..$goal }; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 141 | return { | ||||||||||
| 142 | 0 | Benchmark => $t, | |||||||||
| 143 | goal => $goal, | ||||||||||
| 144 | count => $count, | ||||||||||
| 145 | counter => $counter, | ||||||||||
| 146 | }; | ||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | # literal code-blocks | ||||||||||
| 150 | sub code_literal | ||||||||||
| 151 | 0 | ▶ | { | ||||||||
|
Subroutine
code_literal: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 152 | 0 | my ($options) = @_; | |||||||||
| 153 | |||||||||||
| 154 | 0 | ▶ | my $goal = $options->{fastmode} ? 2_900_000 : 11_600_000; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 0 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | |||||||||||
| 157 | 0 | my $counter; | |||||||||
| 158 | 0 | ▶ | my $t = timeit $count, sub { "1234" =~ /\d+(?{$counter++})/ for 1..$goal }; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 159 | return { | ||||||||||
| 160 | 0 | Benchmark => $t, | |||||||||
| 161 | goal => $goal, | ||||||||||
| 162 | count => $count, | ||||||||||
| 163 | }; | ||||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | # http://stackoverflow.com/questions/29217346/why-does-adding-one-more-alternative-make-my-regex-over-600-times-slower | ||||||||||
| 167 | sub trie_limit | ||||||||||
| 168 | 0 | ▶ | { | ||||||||
|
Subroutine
trie_limit: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 169 | 0 | my ($options) = @_; | |||||||||
| 170 | |||||||||||
| 171 | 0 | ▶ | my $goal = $options->{fastmode} ? 16382 : 16383; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 0 | ▶ | my $count = $options->{fastmode} ? 1 : 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | |||||||||||
| 174 | # things explode *after* $goal=16382 | ||||||||||
| 175 | 0 | my @items = map unpack("H8", pack "V", $_), 0..30_000; # must be greater than $goal | |||||||||
| 176 | 0 | my $nB = $goal; | |||||||||
| 177 | 0 | my $reB = join "|", @items[1 .. $nB]; | |||||||||
| 178 | 0 | $reB = qr/^($reB)/; # anchor and compile regex | |||||||||
| 179 | |||||||||||
| 180 | 0 | my $counter; | |||||||||
| 181 | 0 | ▶ | my $t = timeit $count, sub { $nB == grep /$reB/, @items or die; }; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 182 | return { | ||||||||||
| 183 | 0 | Benchmark => $t, | |||||||||
| 184 | goal => $goal, | ||||||||||
| 185 | count => $count, | ||||||||||
| 186 | }; | ||||||||||
| 187 | } | ||||||||||
| 188 | |||||||||||
| 189 | sub main | ||||||||||
| 190 | 0 | ▶ | { | ||||||||
|
Subroutine
main: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 191 | 0 | my ($options) = @_; | |||||||||
| 192 | |||||||||||
| 193 | 0 | my $results; | |||||||||
| 194 | |||||||||||
| 195 | 0 | eval { | |||||||||
| 196 | 0 | $results = { | |||||||||
| 197 | split1 => split1($options), | ||||||||||
| 198 | split2 => split2($options), | ||||||||||
| 199 | backtrack => backtrack($options), | ||||||||||
| 200 | runtime_comp => runtime_comp($options), | ||||||||||
| 201 | runtime_comp_nocache => runtime_comp_nocache($options), | ||||||||||
| 202 | precomp_access => precomp_access($options), | ||||||||||
| 203 | code_runtime => code_runtime($options), | ||||||||||
| 204 | code_literal => code_literal($options), | ||||||||||
| 205 | trie_limit => trie_limit($options), | ||||||||||
| 206 | }; | ||||||||||
| 207 | 0 | my $splitratio = $results->{split1}{Benchmark}[0] / $results->{split2}{Benchmark}[0]; | |||||||||
| 208 | # fake 'splitratio' as Benchmark.pm structure, just for symmetry | ||||||||||
| 209 | 0 | $results->{splitratio} = {Benchmark => bless([ $splitratio, $splitratio, 0, 0, 0, $results->{split1}{count}], 'Benchmark')}; | |||||||||
| 210 | }; | ||||||||||
| 211 | |||||||||||
| 212 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 213 | 0 | ▶ | warn $@ if $options->{verbose}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 214 | 0 | $results = { failed => $@ }; | |||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | 0 | return $results; | |||||||||
| 218 | } | ||||||||||
| 219 | |||||||||||
| 220 | 1 | 1; | |||||||||
| 221 | |||||||||||
| 222 | __END__ | ||||||||||