KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Given a MATLAB uint32 to be interpreted as a bit string, what is an efficient and concise way of counting how many nonzero bits are in the string? I have a working, naive approach which loops over the bits, but that's too slow for my needs. (A C++ implementation using std::bitset count() runs almost instantly). I've found a pretty nice page listing various bit counting techniques, but I'm hoping there is an easy MATLAB-esque way. http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive Update #1 Just implemented the Brian Kernighan algorithm as follows: w = 0; while ( bits > 0 ) bits = bitand( bits, bits-1 ); w = w + 1; end Performance is still crappy, over 10 seconds to compute just 4096^2 weight calculations. My C++ code using count() from std::bitset does this in subsecond time. Update #2 Here is a table of run times for the techniques I've tried so far. I will update it as I get additional ideas/suggestions. Vectorized Scheiner algorithm => 2.243511 sec Vectorized Naive bitget loop => 7.553345 sec Kernighan algorithm => 17.154692 sec length( find( bitget( val, 1:32 ) ) ) => 67.368278 sec nnz( bitget( val, 1:32 ) ) => 349.620259 sec Justin Scheiner's algorithm, unrolled loops => 370.846031 sec Justin Scheiner's algorithm => 398.786320 sec Naive bitget loop => 456.016731 sec sum(dec2bin(val) == '1') => 1069.851993 sec Comment : The dec2bin() function in MATLAB seems to be very poorly implemented. It runs extremely slow. Comment : The "Naive bitget loop" algorithm is implemented as follows: w=0; fo
Tags (comma-separated)
Save Edits
Cancel