Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: fast bit twiddling

by BUU (Prior)
on May 02, 2004 at 10:39 UTC ( [id://349795]=note: print w/replies, xml ) Need Help??


in reply to fast bit twiddling

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

Replies are listed 'Best First'.
Re: Re: fast bit twiddling
by ysth (Canon) on May 02, 2004 at 10:55 UTC
    Try:
    ysth => sub { my $s = "1010101011"; my $test = sub { (" $s") =~ /^@{[map "(?=.{$_}(?:01|10))" +,@_]}/ +x }; $test->(2,5,8); }
    And properly speaking, the ones that don't support multiple bits should be $test->(2) && $test->(5) && $test->(8). Also not sure why you are using anonymous subs in the code to test.
Re: Re: fast bit twiddling
by BrowserUk (Patriarch) on May 02, 2004 at 14:03 UTC

    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.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      Well, if they are bits, then the number can be done as binary. So translating it from a string to a number isn't evil. :)

      If this were trinary or pentary (base 3 or base 5), it'd be a little more difficult and using a string representation may be better. I have no interest in base 3 or 5, so I haven't tested. ;)

        Agreed. You can pack the bits to form a number and then manipulate them using boolean operators.

        I found two problems with that approach though. The first is converting a list of bit-indices to a (set of) masks to allow the bit manipulations is pretty inefficient.

        Zaxo neatly sidestepped that by taking the list (2, 5, 8) as being a fixed list and performing the conversion once. If that is the case in the OP's requirements than his method (or yours:) will probably be much quicker, but that wasn't my interpretation of the OP.

        The second problem is that whilst the bit-string given as an example only had 10-bits, it was far from clear that this was the limit, or even typical. If the length of the bit-string moves beyond the largest native-integer size, then the boolean manipulations fall down.

        Only the OP can know exactly what form his data takes and therefore which approach is best for his application. If he can obtain/maintain his bits as bits rather than bytes, that would definitely be the way to go.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
Re: Re: fast bit twiddling
by exussum0 (Vicar) on May 02, 2004 at 13:19 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://349795]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 07:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found