比较以下两个表达式
std::bitset<8>(5).count()
__builtin_popcount(5)
Run Code Online (Sandbox Code Playgroud)
哪一个更好?
int __builtin_popcount(unsigned int);
Run Code Online (Sandbox Code Playgroud)
是 GCC 的内置函数,同时std::bitset<N>::count是 C++ 标准。
两个函数都执行相同的操作:返回设置为 的位数true。
你应该用什么?
总是倾向于使用C++标准的函数,因为其他编译器不支持__builtin_popcount函数。
更新
如果你看一下 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);
Run Code Online (Sandbox Code Playgroud)
使用 GCC 9.2 和 flags -std=c++2a -O3,GCC 内置函数比该std::bitset<N>::count()函数慢 10%,但是,由于两个函数的 ASM 输出相同,基准测试的差异可能是由于其他因素造成的。
| 归档时间: |
|
| 查看次数: |
2186 次 |
| 最近记录: |