![]() |
|
good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Bitwise operators on binary objectsby Anonyrnous Monk (Hermit) |
on Jan 31, 2011 at 21:31 UTC ( [id://885368]=note: print w/replies, xml ) | Need Help?? |
Newer processors have a popcnt ("population count") instruction, which computes the number of 1-bits (for up to 64 bits) with a single fast CPU instruction. In other words, if you want real speed at the cost of portability, you could hack a little assembly1,2. A quick test shows that this is roughly 10 times as fast as the above mentioned unpack "%32b*".
Tested with
___ 1 The assembly code (nasm syntax; 64 bit; gcc register subroutine calling convention — compile with nasm -f elf64 popcount.asm ):
(Caveat: as this is just proof of concept code, the string length is assumed to be a multiple of 8 byte/64 bit, so you might need to zero-pad it) 2 In theory, there is also the gcc __builtin_popcount function, but in practice it turns out to be less portable than a piece of assembly code.
In Section
Seekers of Perl Wisdom
|
|