操作长位串的数据结构
在 Python 中,执行通常的二进制运算 (&
) 的 n
位(其中 n
约为 10000)的最佳数据结构是什么? , |
, ^
) 与其他此类数据结构的速度快?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“快”总是相对的:)
BitVector 包似乎可以满足您的需要。不过,我对它的性能没有任何经验。
还有一个 BitString 实现。也许您会进行一些测量来找出哪一种更能满足您的特定需求?
如果您不需要特定的类,也不需要诸如切片或位计数之类的东西,那么您可以简单地使用 python 的任意长度整数的
long
值。这可能是性能最好的实现。这个问题似乎类似,尽管作者需要更少的位并且需要一个标准库。
"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.
除了 MartinStettner 提到的模块之外,还有 bitarray 模块,我已在多个项目上使用过该模块取得良好成果的场合。
PS:我的第100个回答,wohooo!
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!