何时使用 STL 位集而不是单独的变量?

发布于 2024-07-04 09:49:11 字数 118 浏览 10 评论 0原文

在什么情况下,使用位集(STL 容器)来管理一组标志而不是将它们声明为多个单独的(布尔)变量更合适?

如果我使用 50 个标志的位集而不是使用 50 个单独的 bool 变量,我会获得显着的性能提升吗?

In what situation would it be more appropriate for me to use a bitset (STL container) to manage a set of flags rather than having them declared as a number of separate (bool) variables?

Will I get a significant performance gain if I used a bitset for 50 flags rather than using 50 separate bool variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你爱我像她 2024-07-11 09:49:12

当您需要序列化/反序列化 std::bitset 时,它会给您额外的分数。 您可以将其写入流或从流中读取。 但可以肯定的是,单独的布尔值会更快。 毕竟,它们针对这种用途进行了优化,而位集针对空间进行了优化,并且仍然涉及函数调用。 它永远不会比单独的布尔值更快。

Bitset

  • 非常节省空间,
  • 由于位摆弄而效率较低
  • 使用 op<<op>> 提供序列化/反序列化
  • 所有位打包在一起:您将拥有标志在一个地方。

单独的布尔值

  • 非常快
  • 布尔值不打包在一起。 他们将成为某个地方的成员。

根据事实来决定。 就我个人而言,我会使用 std::bitset 来处理一些对性能要求不高的情况,并且如果我只有几个布尔值(因此它非常易于概述),或者如果我需要,我会使用布尔值额外的性能。

std::bitset will give you extra points when you need to serialize / deserialize it. You can just write it to a stream or read from a stream with it. But certainly, the separate bools are going to be faster. They are optimized for this kind of use after all, while a bitset is optimized for space, and has still function calls involved. It will never be faster than separate bools.

Bitset

  • Very space efficient
  • Less efficient due to bit fiddling
  • Provides serialize / de-serialize with op<< and op>>
  • All bits packed together: You will have the flags at one place.

Separate bools

  • Very fast
  • Bools are not packed together. They will be members somewhere.

Decide on the facts. I, personally, would use std::bitset for some not-performance critical, and would use bools if I either have only a few bools (and thus it's quite overview-able), or if I need the extra performance.

梨涡少年 2024-07-11 09:49:12

RE @Wilka:

实际上,C/C++ 支持位集,不需要您自己进行屏蔽。 我不记得确切的语法,但它是这样的:

struct MyBitset {
  bool firstOption:1;
  bool secondOption:1;
  bool thirdOption:1;
  int fourBitNumber:4;
};

您可以仅使用点表示法来引用该结构中的任何值,并且正确的事情将会发生:

MyBitset bits;
bits.firstOption = true;
bits.fourBitNumber = 2;

if(bits.thirdOption) {
  // Whatever!
}

您可以对事物使用任意位大小。 生成的结构最多可以比您定义的数据大 7 位(其大小始终是存储您定义的数据所需的最小字节数)。

RE @Wilka:

Actually, bitsets are supported by C/C++ in a way that doesn't require you to do your own masking. I don't remember the exact syntax, but it's something like this:

struct MyBitset {
  bool firstOption:1;
  bool secondOption:1;
  bool thirdOption:1;
  int fourBitNumber:4;
};

You can reference any value in that struct by just using dot notation, and the right things will happen:

MyBitset bits;
bits.firstOption = true;
bits.fourBitNumber = 2;

if(bits.thirdOption) {
  // Whatever!
}

You can use arbitrary bit sizes for things. The resulting struct can be up to 7 bits larger than the data you define (its size is always the minimum number of bytes needed to store the data you defined).

任性一次 2024-07-11 09:49:12

这取决于您所说的“性能增益”是什么意思。 如果您只需要其中 50 个,并且内存并不低,那么单独的 bool 几乎总是比位集更好的选择。 它们会占用更多内存,但布尔值会快得多。 位集通常被实现为整数数组(布尔值被打包到这些整数中)。 因此,位集中的前 32 个 bool(位)将仅占用一个 32 位 int,但要读取每个值,您必须首先执行一些按位操作,以屏蔽掉所有不需要的值。 例如,要读取位集的第二位,您需要:

  1. 查找包含所需位的 int(在本例中,它是第一个 int)
  2. 按位 并查找带有“2”(即值 & 0x02)的 int 但是,如果

内存是瓶颈并且您有很多布尔值,那么使用位集可能是有意义的(例如,如果您的目标平台是移动电话,或者它是非常繁忙的网络服务中的某种状态)

注意:bool 的 std::vector 通常有专门化使用 相当于一个位集,从而使其更小并且由于同样的原因也更慢。 因此,如果速度是一个问题,那么最好使用 char(甚至 int)向量,甚至只使用老式的 bool 数组。

It depends what you mean by 'performance gain'. If you only need 50 of them, and you're not low on memory then separate bools is pretty much always a better choice than a bitset. They will take more memory, but the bools will be much faster. A bitset is usually implemented as an array of ints (the bools are packed into those ints). So the first 32 bools (bits) in your bitset will only take up a single 32bit int, but to read each value you have to do some bitwise operations first to mask out all the values you don't want. E.g. to read the 2nd bit of a bitset, you need to:

  1. Find the int that contains the bit you want (in this case, it's the first int)
  2. Bitwise And that int with '2' (i.e. value & 0x02) to find out if that bit is set

However, if memory is a bottleneck and you have a lot of bools using a bitset could make sense (e.g. if you're target platform is a mobile phone, or it's some state in a very busy web service)

NOTE: A std::vector of bool usually has a specialisation to use the equivalent of a bitset, thus making it much smaller and also slower for the same reasons. So if speed is an issue, you'll be better off using a vector of char (or even int), or even just use an old school bool array.

百合的盛世恋 2024-07-11 09:49:11

那么,50 个布尔值作为位集将占用 7 个字节,而 50 个布尔值作为布尔值将占用 50 个字节。 现在这已经不是什么大问题了,所以使用 bool 可能就可以了。

然而,位集可能有用的地方是,如果您需要大量传递这些布尔值,特别是如果您需要从函数返回该集合。 使用位集,您需要在堆栈上移动才能返回的数据就会减少。 话又说回来,您可以只使用 refs 来代替,并且需要传递的数据甚至更少。 :)

Well, 50 bools as a bitset will take 7 bytes, while 50 bools as bools will take 50 bytes. These days that's not really a big deal, so using bools is probably fine.

However, one place a bitset might be useful is if you need to pass those bools around a lot, especially if you need to return the set from a function. Using a bitset you have less data that has to be moved around on the stack for returns. Then again, you could just use refs instead and have even less data to pass around. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文