I did some benchmarking of this solution, comparing it to a dumb join '|', 0 .. 255.
Update: and benchmarked mwah's solution as well.
The non-backtracking solution really pays off:
Rate brute_force mwah grinder
brute_force 125/s -- -25% -92%
mwah 167/s 33% -- -89%
grinder 1520/s 1114% 812% --
However demerphq++'s Trie optimization (I hope I remembered the name correctly) in perl5.10 reduced that advantage significantly - so far, that it becomes faster than the code assertions:
Rate mwah brute_force grinder
mwah 156/s -- -32% -87%
brute_force 229/s 47% -- -81%
grinder 1181/s 659% 416% --
Removing the /o-flags from the regexes makes the gap a bit wider.
| [reply] [d/l] [select] |
moritz I used this script for benchmarking:
Thanks for doing this work already (I started to think about that too;-)
As it looks, the dynamic code assertion *is* somehow slow in this context. I suspected this but it'd be interesting if this disadvantage starts somewhere to disappear eventually.
I 'compacted' your benchmark code a bit and tried to expand the context, eg. to
match on all numbers from 0..10,000 which are below 2,567.
This was easily made into the bruteforce and mwah variants but I failed to put this (working) into the 'grinder-sub' (maybe somebody can try). On a larger range, it's clear that the bruteforce-sub approach starts to fail. The beauty of the dynamic code assertion (its slowness will somehow 'disappear' on larger ranges) is the expressiveness which is somehow in contrast to the 'reinvention' of number parser (grinder-sub). Of course, if the problematic range is constant and the developer is able to provide a non-backtracking 'grinder-like' solution, this can't be beaten by anything. This is my shortened variant (w/grinder defunct) version of your benchmark code:
...
use Benchmark qw(:all);
our $bruteforce_re = 0; # give it the extra preparation bonus
my $grinder = sub {
my $fails = 0;
for( 0 .. 10000 ) {
++$fails if ! /^(?:2(?:[6-9]|5[0-5]?|[0-4]\d?)?|[3-9]\d?|1(?:\d\
+d?)?|0)$/o
}
die "grinder $fails:" if $fails != 10000-2566 # did we get this
+right?
};
my $mwah = sub {
my $fails = 0;
my $re = qr{^(\d+)$(??{$1<=2566?'':'(?!)'})}x;
for( 0 .. 10000 ) {
++$fails if ! /$re/
}
die "mwah $fails:" if $fails != 10000-2566 # did we get this
+right?
};
my $bruteforce = sub {
$bruteforce_re = '^(?:' . join('|', 0 .. 2566) . ')$' unless $brute
+force_re;
my $fails = 0;
for( 0 .. 10000 ) {
++$fails if ! /$bruteforce_re/o
}
die "bruteforce $fails:" if $fails != 10000-2566 # did we get this
+right?
};
cmpthese(-3, {# grinder => $grinder,
mwah => $mwah,
brute_force => $bruteforce,
});
...
In this range, the dynamic code assertion is already 5 times faster (here) than the bruteforce approach.
Regards
mwa | [reply] [d/l] |
In production code, it might be worth expanding grinder's regexp to
use warnings;
use strict;
my @test_values = qw/ 26 231 232 233 234 254 255 256 025 0000 /;
my $rx = qr /^ (?:
2 # first digit is a 2
(?:
[6-9] # second and _last_ digit is
+ 6-9
| 5 # or second digit 5
[0-5]? # maybe third and _last_ dig
+it 0-5
| [0-4] # or second digit 0-4
\d? # maybe followed by a final
+third 0-9
)?
| [3-9] # first digit 3-9
\d? # maybe followed by final 0-
+9
| 1 # first digit 1
(?:
\d\d? # maybe followed by 1 or 2 digi
+ts
)?
| 0 # single-digit 0
)
$/x;
foreach my $number ( @test_values ) {
print "$number:",
($number =~ /$rx/ ? 'true' : 'false'),
"\n",
;
}
Which makes it easier to see that leading zeroes, as in 0000 or 025, wouldn't fit. That might be or not a problem, of course.
| [reply] [d/l] |