In Python, what is the best data structure of n bits (where n is about 10000) on which performing the usual binary operations (&, |, ^) with other such data structures is fast?

有帮助吗?

解决方案

"Fast" is always relative :)

The BitVector package seems to do what you need. I have no experience concerning performance with it though.

There is also a BitString implementation. Perhaps you do some measurements to find out, which one is more performant for your specific needs?

If you don't want a specific class and do not need things such as slicing or bit counting, you might be ok simply using python's long values which are arbitrary length integers. This might be the most performant implementation.

This qestion seems to be similar, although the author need fewer bits and requires a standard library.

其他提示

In addition to those mentioned by MartinStettner, there's also the bitarray module, which I've used on multiple occations with great results.

PS: My 100th answer, wohooo!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top