std::bitset<N>::count vs __builtin_popcount

人走茶凉 提交于 2021-02-13 17:03:59

问题


Comparing the following two expressions

std::bitset<8>(5).count()
__builtin_popcount(5)

which one is better?


回答1:


int  __builtin_popcount(unsigned int);

is a built in function of GCC while std::bitset<N>::count is a C++ standard.

Both function do the same thing: return the number of bits that are set to true.

What should you use?

Always tend to use C++ standard's functions because other compilers don't support __builtin_popcount function.

UPDATE

If you take a look at the statistics made by Google Benchmark tool:

#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.




回答2:


According to godbolt, bitset and popcount yields just the same asm output on latest g++. 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. Therefore, bitset option is clearly better.



来源:https://stackoverflow.com/questions/60165922/stdbitsetncount-vs-builtin-popcount

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!