Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-19 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found