其中有一段数据是存在依赖的,是不能矢量化的,注释掉看一下效果吧。
/*
*Name : vectorized.c
*Purpose: test the performance of intel compiler
*Author: Albert
*/
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
const int MAX = 2000000;
const int ITERMAX = 20000;
int main(void) {
int i, iter,arr[MAX];
timeval start,end;
long long tPassed = 0;
memset(arr, 0, sizeof(arr));
gettimeofday(&start,0);
for (iter = 1; iter < ITERMAX; iter++) {
// for(i = 1; i < MAX; i++){
// arr[i] = arr[i-1] + 1;
// }
for(i = 1; i < MAX; i++){
arr[i] += 1;
}
}
gettimeofday(&end,0);
end.tv_sec -= start.tv_sec;
end.tv_usec -= start.tv_usec;
tPassed = 1000000LL * end.tv_sec + end.tv_usec;
tPassed /= 1000;
printf("%lld ms\n",tPassed);
return 0;
}
环境:
Linux v3901 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:19 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux,32GB内存,Intel(R) Xeon(R) CPU E5450 @ 3.00GHz,8核,L1-Cahce 6144 KB.
[scwangj@v3901 simple]$ icpc vectorized.c -o ivectorized
vectorized.c(19): (col. 2) remark: PERMUTED LOOP WAS VECTORIZED.
[scwangj@v3901 simple]$ g++ vectorized.c -o vectorized
[scwangj@v3901 simple]$ ./ivectorized
3696 ms
[scwangj@v3901 simple]$ ./vectorized
114530 ms
时间上快了30倍还要多!个人觉得这个结果超过了预期的16倍,不太靠谱,原因在于测试程序不合适,表达式过于简单,等有功夫了再改吧