STL bitset::count() 方法的性能如何?

发布于 2024-10-12 01:49:45 字数 113 浏览 5 评论 0原文

我四处搜寻,找不到 bitset::count() 的性能时间规范。有谁知道它是什么(O(n) 或更好)以及在哪里可以找到它?

编辑通过STL我仅指标准模板库。

I searched around and could not find the performance time specifications for bitset::count(). Does anybody know what it is (O(n) or better) and where to find it?

EDIT By STL I refer only to the Standard Template Library.

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

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

发布评论

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

评论(5

書生途 2024-10-19 01:49:45

我在我的计算机上读取了这个文件(C:\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\bitset)。
顺便说一句,请参阅这些

/// Returns the number of bits which are set.
size_t
count() const { return this->_M_do_count(); }      

size_t
_M_do_count() const
{
  size_t __result = 0;
  for (size_t __i = 0; __i < _Nw; __i++)
  __result += __builtin_popcountl(_M_w[__i]);
  return __result;
}

,这是指定 _Nw 的地方:

  template<size_t _Nw>
    struct _Base_bitset

因此在 gcc 实现中它是 O(n) 。我们得出的结论是,规范并不要求它比 O(n) 更好。任何头脑正常的人都不会以比这更糟糕的方式来实施它。然后我们可以放心地假设最坏的情况是 O(n)。可能会更好,但你永远不能指望这一点。

I read this file (C:\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\bitset) on my computer.
See these

/// Returns the number of bits which are set.
size_t
count() const { return this->_M_do_count(); }      

size_t
_M_do_count() const
{
  size_t __result = 0;
  for (size_t __i = 0; __i < _Nw; __i++)
  __result += __builtin_popcountl(_M_w[__i]);
  return __result;
}

BTW, this is where _Nw is specified:

  template<size_t _Nw>
    struct _Base_bitset

Thus it's O(n) in gcc implementation. We conclude the specification doesn't require it better than O(n). And nobody in their right mind will implement it in a way worse than that. We can then safely assume that it's at worst O(n). Possibly better but you can never count on that.

晨曦÷微暖 2024-10-19 01:49:45

由于 C++ 社区中普遍滥用该术语,我无法确定您在这里所说的“STL”的真正含义。

  • C++ 标准 (2003) 没有强制要求std::bitset::count() 的性能(或者,事实上,据我所知,std::bitset 的任何成员)。

  • 我也找不到任何建议对 STL 的 bitset::count() 性能进行强制要求的参考资料。

不过,我认为任何理智的实现都会在恒定(或最坏的线性)时间内提供此功能。然而,这只是一种感觉。检查一下你的,看看你实际上会得到什么。

I can't be sure what you really mean by "STL" here, due to a prevailing misuse of the term in the C++ community.

  • The C++ Standard (2003) makes no mandate for the performance of std::bitset::count() (or, in fact, any members of std::bitset as far as I can see).

  • I can't find any reference suggesting a mandate for the performance of STL's bitset::count() either.

I think any sane implementation will provide this in constant (or at worst linear) time, though. However, this is merely a feeling. Check yours to find out what you'll actually get.

心作怪 2024-10-19 01:49:45

“SGI 的参考实现运行
在线性时间内相对于
需要存储的字节数
位。它通过创建一个
256 个整数的静态数组。这
存储在数组中第 i 个索引处的值
是值中设置的位数
我。”

http://www.cplusplus.com/forum/general/12486/

"SGI's reference implementation runs
in linear time with respect to the
number of bytes needed to store the
bits. It does this by creating a
static array of 256 integers. The
value stored at ith index in the array
is the number of bits set in the value
i."

http://www.cplusplus.com/forum/general/12486/

煮酒 2024-10-19 01:49:45

我不确定您是否会找到相应的规范,因为 STL 通常不需要一定程度的性能。我已经看到暗示它“快”,在集合的大小中每个比特大约 1 个周期。您当然可以阅读特定实现的代码以了解所期望的内容。

I'm not sure you're going to find a specification for that, since the STL doesn't typically require a certain level of performance. I've seen hints that it's "fast", around 1 cycle per bit in the set's size. You can of course read your particular implementation's code to find out what to expect.

少钕鈤記 2024-10-19 01:49:45

我们遵循的算法是对所有设置为 1 的位进行计数。
现在,如果我们想对数字 n 的位集进行计数,我们将遍历 log(n)+1 位数字。

例如:对于数字 13,我们得到 1101 作为位集。

13 的自然对数 = 2.564(大约) 3

位数 = 3+1 = 4

对于任何数字 n(十进制),我们循环 log(n)+1 次。

另一种方法如下:

int count_set_bits_fast(int n) {
int count = 0;
while (n > 0) {
    n=(n&(n-1));
    count++
}

return count;
}

如果分析功能线 n=(n&(n-1));你会发现它本质上减少了从右到左的位数。

因此,顺序将是总设置位数。

例如: 13 = 1101

1101&1100 = 1100

1100&1011 = 1000

1000&0111 = 0

O(设置位数), O(Log(n)+1) 最坏情况

The Algorithm that we follow is to count all the bits that are set to 1.
Now if we want to count through that bitset for a number n, we would go through log(n)+1 digits.

For example: for the number 13, we get 1101 as the bitset.

Natural log of 13 = 2.564 (approximately) 3

Number of bits = 3+1 = 4

For any number n(decimal) we loop log(n)+1 times.

Another approach would be the following:

int count_set_bits_fast(int n) {
int count = 0;
while (n > 0) {
    n=(n&(n-1));
    count++
}

return count;
}

If you analyse the functional line n=(n&(n-1)); you shall find that it essentially reduces the number of bits from right to left.

The Order would therefore be number of total set bits.

For example: 13 = 1101

1101&1100 = 1100

1100&1011 = 1000

1000&0111 = 0

O(number of set bits), O(Log(n)+1) Worst case

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