C# 为什么 BitVector 32 结构比 BitArray 更高效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/903548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 01:50:47  来源:igfitidea点击:

Why BitVector 32 structure is more efficient than BitArray?

c#collectionsbitvector

提问by Jaywith.7

What is the difference between BitArray and BitVector 32 structure and what are the advantages of BitVector 32 structure over BitArray? Why is the BitVector 32 structure more efficient than BitArray?

BitArray 和 BitVector 32 结构有什么区别,BitVector 32 结构相对于 BitArray 有什么优势?为什么 BitVector 32 结构比 BitArray 更高效?

Thanks in advance.

提前致谢。

Jay...

杰...

回答by Sinan ünür

Here is what Microsoft's documentation for BitVector32states:

以下是Microsoft 的 BitVector32 文档所述:

BitVector32is more efficient than BitArrayfor Boolean values and small integers that are used internally. A BitArraycan grow indefinitely as needed, but it has the memory and performance overhead that a class instance requires. In contrast, a BitVector32uses only 32 bits.

BitVector32BitArray内部使用的布尔值和小整数更有效。ABitArray可以根据需要无限增长,但它具有类实例所需的内存和性能开销。相比之下, aBitVector32仅使用 32 位。

The capacity of BitVector32is limited to 32 bits, the size of an int. Therefore, indexing and masking can be single operations. Compare this to a bit array with 734 bits and you want to find out if bit 197 is set. Think about how you would do that (from the perspective of the class designer).

的容量BitVector32限制为 32 位,大小为int. 因此,索引和屏蔽可以是单个操作。将此与具有 734 位的位数组进行比较,您想知道是否设置了位 197。想想你会怎么做(从类设计者的角度)。

回答by Matt Brindley

BitVector32 is more efficient than BitArray for Boolean values and small integers that are used internally. A BitArray can grow indefinitely as needed, but it has the memory and performance overhead that a class instance requires. In contrast, a BitVector32 uses only 32 bits.

对于内部使用的布尔值和小整数,BitVector32 比 BitArray 更有效。BitArray 可以根据需要无限增长,但它具有类实例所需的内存和性能开销。相比之下,BitVector32 仅使用 32 位。

http://msdn.microsoft.com/en-us/library/system.collections.specialized.bitvector32.aspx

http://msdn.microsoft.com/en-us/library/system.collections.specialized.bitvector32.aspx

BitVector32 is a struct and consumes only 4 bytes. BitArray is a class that has overheads associated with it and is therefore less efficient - BitArray will need at least 8 bytes before you've even added any objects to it as it lives on the heap. More about the stack and heap here.

BitVector32 是一个结构体,仅占用 4 个字节。BitArray 是一个有相关开销的类,因此效率较低 - BitArray 需要至少 8 个字节才能向它添加任何对象,因为它位于堆上。有关堆栈和堆的更多信息,请参阅此处

回答by Trisped

A BitVector32gets it boost over BitArraybecause it is just a 32 bit integer and does not have the overhead associated with a class (mainly the memory overhead).

ABitVector32得到了提升,BitArray因为它只是一个 32 位整数并且没有与类相关的开销(主要是内存开销)。

This means if you need to store more then 32 Boolean values then you will either need to use BitArrayor multiple BitVector32. Since multiple BitVector32might be cumbersum you might want to put them into an array or a class, which would remove the performance boost.

这意味着如果您需要存储超过 32 个布尔值,那么您将需要使用BitArray或多个BitVector32. 由于多个BitVector32可能是繁琐的,您可能希望将它们放入数组或类中,这将消除性能提升。

In short, if you need to store 32 or less Boolean values then use a BitVector32. If you need to store more then evaluate your needs and coding conditions before blindly picking BitVector32, otherwise you might make more work for yourself reinventing BitArrayand not see any of the performance benefits.

简而言之,如果您需要存储 32 个或更少的布尔值,则使用BitVector32. 如果您需要存储更多,请在盲目选择之前评估您的需求和编码条件BitVector32,否则您可能会为自己重新发明做更多的工作BitArray而看不到任何性能优势。

Note: in most cases I prefer using a flagged enuminstead of a BitVectore32. See this questionfor an explanation and some good tricks.

注意:在大多数情况下,我更喜欢使用带标记的枚举而不是BitVectore32. 请参阅此问题以获取解释和一些好的技巧。