我的代码如下,我使用 GCC 4.8.2:
#include <iostream>
#include <stdint.h>
#include <sys/time.h>
#include <ctime>
using namespace std;
int main(int argc, char *argv[]) {
struct timespec time_start={0, 0},time_end={0, 0};
uint8_t bitmap[20240];
int cost;
clock_gettime(CLOCK_REALTIME, &time_start);
for (int i = 0; i < 20240; ++i) {
bitmap[i >> 3] |= 1 << (i&7);
}
clock_gettime(CLOCK_REALTIME, &time_end);
cost = time_end.tv_nsec - time_start.tv_nsec;
cout << "case COST: " << cost << endl;
clock_gettime(CLOCK_REALTIME, &time_start);
for (int i = 0; i < 20240; ++i) {
bitmap[i >> 3] &= 1 << (i&7);
}
clock_gettime(CLOCK_REALTIME, &time_end);
cost = time_end.tv_nsec - time_start.tv_nsec;
cout << "case COST: " << cost << endl;
int a = bitmap[1];
std::cout << "TEST: " << a << endl;
}
我用它编译
gcc -lstdc++ -std=c++11 -O2 -ftree-vectorize -ftree-vectorizer-verbose=7 -fopt-info test.cpp
我明白了test.cpp:14: note: not vectorized: not enough data-refs in basic block.
。
然后我运行二进制文件a.out
并得到COST
超过 20000 个。
如果我 delete std::cout << "TEST: " << a << endl;
,则此代码是矢量化的并且COST
小于 100。
任何人都可以帮助我。