[英]std::bitset<N>::count vs __builtin_popcount
Comparing the following two expressions比较以下两个表达式
std::bitset<8>(5).count()
__builtin_popcount(5)
which one is better?哪一个更好?
According to godbolt, bitset and popcount yields just the same asm output on latest g++.根据 Godbolt 的说法, bitset和popcount在最新的 g++ 上产生相同的 asm 输出。 However, as mentioned in the comments,
__builtin_popcount
is an gcc extension and won't be available on neither other compilers nor other architectures than x86.但是,正如评论中提到的,
__builtin_popcount
是一个 gcc 扩展,并且在 x86 以外的其他编译器或其他架构上都不可用。 Therefore, bitset option is clearly better.因此,bitset 选项显然更好。
int __builtin_popcount(unsigned int);
is a built in function of GCC while std::bitset<N>::count
is a C++ standard.是 GCC 的内置函数,而
std::bitset<N>::count
是 C++ 标准。
Both function do the same thing: return the number of bits that are set to true
.两个函数都做同样的事情:返回设置为
true
的位数。
What should you use?你应该用什么?
Always tend to use C++ standard's functions because other compilers don't support __builtin_popcount
function.总是倾向于使用 C++ 标准的函数,因为其他编译器不支持
__builtin_popcount
函数。
UPDATE更新
If you take a look at the statistics made by Google Benchmark tool:如果您查看 Google Benchmark 工具的统计数据:
#include <bitset>
static void GccBuiltInPopCount(benchmark::State& state) {
for (auto _ : state) {
__builtin_popcount(5);
}
}
BENCHMARK(GccBuiltInPopCount);
static void StdBitsetCount(benchmark::State& state) {
for (auto _ : state) {
std::bitset<8>(5).count();
}
}
BENCHMARK(StdBitsetCount);
with GCC 9.2 and flags -std=c++2a -O3
, GCC built in function is 10% slower than the std::bitset<N>::count()
function but, since the ASM output is the same for both function, the difference in benchmark could be due to other factors.使用 GCC 9.2 和标志
-std=c++2a -O3
,GCC 内置函数比std::bitset<N>::count()
函数慢 10% 但是,因为两个函数的 ASM 输出相同,基准的差异可能是由于其他因素造成的。
When you don't know the value of N in std::bitset<N>::count
, I think the second one is better当你不知道
std::bitset<N>::count
中 N 的值时,我认为第二个更好
update: you can try std::popcount更新:你可以试试std::popcount
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.