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); } });

In reply to Re: fast bit twiddling by BUU
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.