سؤال

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