Counting bits in large string / bit vector

Arnaud Delobelle arnodel at gmail.com
Mon Sep 26 12:02:02 CEST 2011


>>>> b = numpy.zeros(10**7, dtype=bool)
>>>> for x in 3, 4, 6: b[10**x] = True
> ...
>>>> b.sum()
> 3

Without numpy:

>>> counts = [bin(i).count('1') for i in range(256)]
>>> bytes = b"hello python"*100000
>>> len(bytes)*8
9600000
>>> sum(map(counts.__getitem__, bytes))
4800000

Pretty fast as well.

-- 
Arnaud



More information about the Python-list mailing list