Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: fast bit twiddling

by BrowserUk (Pope)
on May 02, 2004 at 14:03 UTC ( #349815=note: print w/ replies, xml ) Need Help??


in reply to Re: fast bit twiddling
in thread fast bit twiddling

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


Comment on Re: Re: fast bit twiddling
Download Code
Re: Re: Re: fast bit twiddling
by exussum0 (Vicar) on May 02, 2004 at 23:47 UTC
    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2014-09-17 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (55 votes), past polls