Like ysth's, mine, matija's and japhy's are all intended to take a string, and a list of bits to test as described in the OP. Zaxo's and sporty's seem to be addressing a different (though not necessarily wrong) interpretation of the OP and would need adapting before they were comparable.

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% --

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Re: Re: fast bit twiddling by BrowserUk
in thread fast bit twiddling by spurperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.