STL bitset::count() 方法的性能如何?
我四处搜寻,找不到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在我的计算机上读取了这个文件(C:\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\bitset)。
顺便说一句,请参阅这些
,这是指定 _Nw 的地方:
因此在 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
BTW, this is where _Nw is specified:
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.
由于 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 ofstd::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.
http://www.cplusplus.com/forum/general/12486/
http://www.cplusplus.com/forum/general/12486/
我不确定您是否会找到相应的规范,因为 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.
我们遵循的算法是对所有设置为 1 的位进行计数。
现在,如果我们想对数字 n 的位集进行计数,我们将遍历 log(n)+1 位数字。
例如:对于数字 13,我们得到 1101 作为位集。
13 的自然对数 = 2.564(大约) 3
位数 = 3+1 = 4
对于任何数字 n(十进制),我们循环 log(n)+1 次。
另一种方法如下:
如果分析功能线 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:
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