And it's benchmark time!
The results are directly below, the code used to generated the results is in the readmore tags. Note that I was a tad confused at exactly how to use a couple (namely
ysth's!) so I had to guess and I might not be using them correctly. Also note that I took the easiest way to try to cram it in to a benchmarking format and I might have done something that screwed up the results. If you want to submit another bit of code in the form
name => sub { } that I can more easily/effectively bench, I'd be glad to rebenchmark.
Results:
Rate japhy ysth matija zaxo browser
+uk2
japhy 8361/s -- -37% -74% -82% -
+87%
ysth 13309/s 59% -- -58% -71% -
+79%
matija 31862/s 281% 139% -- -31% -
+49%
zaxo 46230/s 453% 247% 45% -- -
+26%
browseruk2 62527/s 648% 370% 96% 35%
+ --
Benchmark Code:
use strict;
use Benchmark qw(:all);
cmpthese( 1_000_000,
{
zaxo =>
sub {
vec(my $str, 2, 1) = 1;
vec($str, 5, 1) = 1;
vec($str, 8, 1) = 1;
my $test = sub {
my $foo = shift || $_;
my $bar = $foo << 1;
not ($foo & $str) ^ ($bar & $str);
};
$test->(2);
$test->(5);
$test->(8);
},
japhy =>
sub {
my $str = "1010101011";
my $test = sub {
my $l = 0;
$str =~ (
"^" . join "", map "[01]" x ($_ - ($l+0,$l=$_)[0]
+- 2) . "(?:01|10)", @_
);
};
$test->(2);
$test->(5);
$test->(8);
},
matija =>
sub {
my %res=("00"=>0, "01"=>1,"10"=>1,"11"=>0);
my $string = "1010101011";
my $test = sub {
my $tmpres=1;
foreach (@_) {
$tmpres=$tmpres && $res{substr($string,$_-1,2)};
}
return $tmpres;
};
$test->(2);
$test->(5);
$test->(8);
},
browseruk2 =>
sub {
my $s = "1010101011";
my $test = sub {
my $x = shift;
substr $s, $x-1, 1 eq substr $s, $x, 1 and return for
+@_;
return 1;
};
$test->(2);
$test->(5);
$test->(8);
},
ysth =>
sub {
my $s = "1010101011";
my $test = sub { (" ".shift) =~ /^@{[map "(?=.{$_}(?:01|1
+0))",@_]}/x };
$test->($s,2);
$test->($s,5);
$test->($s,8);
}
});