I also found your method of benchmarking somewhat eclectic:), so I did my own which also tests some failing cases and a few different sets of bits. I believe the included cases are all comparable, but I'm open to the possibility that zaxo & sporty interpreted the problem more correctly.
#! perl -slw
use strict;
use Benchmark qw[ cmpthese ];
sub buk1{
my $s = shift;
substr( $s, $_-1, 2 ) =~ m[^(.)\1] and return for @_;
return 1;
}
sub buk2{
my $s = shift;
substr $s, $_-1, 1 eq substr $s, $_, 1 and return for @_;
return 1;
}
sub japhy {
my $str = shift;
my $l = 0;
$str =~ (
"^" . join "", map "[01]" x ($_ - ($l+0,$l=$_)[0] - 2) . "(?:01|10
+)", @_
);
}
{
my %res=("00"=>0, "01"=>1,"10"=>1,"11"=>0);
sub matija{
my $string = shift;
my $tmpres=1;
foreach (@_) {
$tmpres=$tmpres && $res{substr($string,$_-1,2)};
}
return $tmpres;
}
}
{
vec(my $str, 2, 1) = 1;
vec($str, 5, 1) = 1;
vec($str, 8, 1) = 1;
sub zaxo {
my $foo = shift;
my $bar = $foo << 1;
not ($foo & $str) ^ ($bar & $str);
}
}
sub ysth {
(" ".shift) =~ /^@{[map "(?=.{$_}(?:01|10))", @_ ]}/x
}
our @strings = qw[ 0000000000 1111111111 1010101111 1010101011 ];
our @bitmasks = ( [ 2, 5, 8 ], [ 1, 3, 5, 5, 7 ], [ 2, 4, 7, 8, 10 ] )
+;
cmpthese( -3, {
buk1 => q[
for my $string ( @strings ) { buk1 $string, @$_ for @bitmask
+s }
],
buk2 => q[
for my $string ( @strings ) { buk2 $string, @$_ for @bitmask
+s }
],
japhy => q[
for my $string ( @strings ) { japhy $string, @$_ for @bitmask
+s }
],
matija => q[
for my $string ( @strings ) { matija $string, @$_ for @bitmask
+s }
],
# zaxo => q[
# for my $string ( @strings ) { zaxo $string, @$_ for @bitmas
+ks }
# ],
ysth => q[
for my $string ( @strings ) { ysth $string, @$_ for @bitmask
+s }
],
});
__END__
P:\test>349759
Use of uninitialized value in vec at P:\test\349759.pl line 38.
Use of uninitialized value in scalar assignment at P:\test\349759.pl l
+ine 38.
Rate ysth japhy matija buk1 buk2
ysth 1976/s -- -14% -86% -88% -89%
japhy 2289/s 16% -- -84% -86% -87%
matija 14241/s 621% 522% -- -16% -19%
buk1 16920/s 756% 639% 19% -- -4%
buk2 17571/s 789% 668% 23% 4% --