c++ - 为什么 __builtin_popcount 比我自己的位计数功能慢?
问题描述
在我编写了自己的位计数例程之后,我偶然发现了 gcc 的 __builtin_popcount。但是当我切换到 __builtin_popcount 时,我的软件实际上运行得更慢了。我在 Intel Core i3-4130T CPU @ 2.90GHz 上使用 Unbutu。我建立了一个性能测试来看看给出了什么。它看起来像这样:
#include <iostream>
#include <sys/time.h>
#include <stdint.h>
using namespace std;
const int bitCount[256] = {
0,1,1,2,1,2,2,3, 1,2,2,3,2,3,3,4, 1,2,2,3,2,3,3,4, 2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4, 2,3,3,4,3,4,4,5, 2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4, 2,3,3,4,3,4,4,5, 2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6, 3,4,4,5,4,5,5,6, 4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4, 2,3,3,4,3,4,4,5, 2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6, 3,4,4,5,4,5,5,6, 4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6, 3,4,4,5,4,5,5,6, 4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6, 4,5,5,6,5,6,6,7, 4,5,5,6,5,6,6,7, 5,6,6,7,6,7,7,8
};
const uint32_t m32_0001 = 0x000000ffu;
const uint32_t m32_0010 = 0x0000ff00u;
const uint32_t m32_0100 = 0x00ff0000u;
const uint32_t m32_1000 = 0xff000000u;
inline int countBits(uint32_t bitField)
{
return
bitCount[(bitField & m32_0001) ] +
bitCount[(bitField & m32_0010) >> 8] +
bitCount[(bitField & m32_0100) >> 16] +
bitCount[(bitField & m32_1000) >> 24];
}
inline long long currentTime() {
struct timeval ct;
gettimeofday(&ct, NULL);
return ct.tv_sec * 1000000LL + ct.tv_usec;
}
int main() {
long long start, delta, sum;
start = currentTime();
sum = 0;
for(unsigned i = 0; i < 100000000; ++i)
sum += countBits(i);
delta = currentTime() - start;
cout << "countBits : sum=" << sum << ": time (usec)=" << delta << endl;
start = currentTime();
sum = 0;
for(unsigned i = 0; i < 100000000; ++i)
sum += __builtin_popcount(i);
delta = currentTime() - start;
cout << "__builtin_popcount: sum=" << sum << ": time (usec)=" << delta << endl;
start = currentTime();
sum = 0;
for(unsigned i = 0; i < 100000000; ++i) {
int count;
asm("popcnt %1,%0" : "=r"(count) : "rm"(i) : "cc");
sum += count;
}
delta = currentTime() - start;
cout << "assembler : sum=" << sum << ": time (usec)=" << delta << endl;
return 0;
}
起初,我使用较旧的编译器运行它:
> g++ --version | head -1
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
> cat /proc/cpuinfo | grep 'model name' | head -1
model name : Intel(R) Core(TM) i3-4130T CPU @ 2.90GHz
> g++ -O3 popcountTest.cpp
> ./a.out
countBits : sum=1314447104: time (usec)=148506
__builtin_popcount: sum=1314447104: time (usec)=345122
assembler : sum=1314447104: time (usec)=138036
如您所见,基于表的 countBits 几乎与汇编程序一样快,并且远快于 __builtin_popcount。然后我在不同的机器类型上尝试了一个更新的编译器(相同的处理器——我认为主板也是一样的):
> g++ --version | head -1
g++ (Ubuntu 7.3.0-16ubuntu3) 7.3.0
> cat /proc/cpuinfo | grep 'model name' | head -1
model name : Intel(R) Core(TM) i3-4130T CPU @ 2.90GHz
> g++ -O3 popcountTest.cpp
> ./a.out
countBits : sum=1314447104: time (usec)=164247
__builtin_popcount: sum=1314447104: time (usec)=345167
assembler : sum=1314447104: time (usec)=138028
奇怪的是,旧的编译器比新的编译器优化了我的 countBits 函数,但它仍然比汇编器好。显然,由于汇编程序行编译并运行,我的处理器支持 popcount,但为什么 __builtin_popcount 慢两倍以上?我自己的例程怎么可能与基于硅的 popcount 竞争?我在寻找第一个设置位等其他例程方面有相同的经验。我的例程都比 GNU“内置”等效项快得多。
(顺便说一句,我不知道如何编写汇编程序。我只是在某个网页上发现了那行,它似乎奇迹般地工作。)
解决方案
如果没有在命令行上指定适当的“-march”,gcc 会生成对__popcountdi2
函数而不是popcnt
指令的调用。见:https ://godbolt.org/z/z1BihM
根据维基百科,自 Nehalem 以来英特尔和 AMD 自巴塞罗那以来都支持 POPCNT:https ://en.wikipedia.org/wiki/SSE4#POPCNT_and_LZCNT
推荐阅读
- active-directory - Get-ADUser - 搜索过期帐户。在命令中使用变量
- ffmpeg - 如何从 .mpd 播放列表文件下载视频
- oracle - 在案例查询中基于计数返回值
- ios - 'UIAlertView' 已弃用:首先在 iOS 9.0 中弃用
- flutter - 添加了获取它的依赖项。IDE给出版本解决错误
- python - 如何在 Tkinter Entry 小部件中一次生成多个字符?
- c# - 忽略 StreamReader 的 HttpRequest 字段
- linux - Linux 内核源代码中 PCI Core C 文件的位置和名称
- grpc - 如何通过 http 将 .zip 文件下载到 grpc 转码端点?
- python - Python MIP,生成器对象没有属性意义