标签:
1 #include <omp.h> 2 #include <stdio.h> 3 #include <sys/time.h> 4 void test() { 5 int a = 0; 6 for(int i = 0; i < 10000000; i++) { 7 a = i + 1; 8 } 9 } 10 int main() { 11 int nthrds = 4; 12 omp_set_num_threads(nthrds); 13 timeval tStart,cTime; 14 long long tPassed = 0; 15 gettimeofday(&tStart, 0); 16 for (int i = 0; i < 100; i++) { 17 test(); 18 } 19 gettimeofday(&cTime, 0); 20 cTime.tv_sec -= tStart.tv_sec; 21 cTime.tv_usec -= tStart.tv_usec; 22 tPassed = 1000000LL * cTime.tv_sec + cTime.tv_usec; 23 tPassed /= 1000; 24 printf("Time = %lld/n", tPassed); 25 gettimeofday(&tStart, 0); 26 #pragma omp parallel for 27 for (int i = 0; i < 100; i++) { 28 test(); 29 } 30 gettimeofday(&cTime, 0); 31 cTime.tv_sec -= tStart.tv_sec; 32 cTime.tv_usec -= tStart.tv_usec; 33 tPassed = 1000000LL * cTime.tv_sec + cTime.tv_usec; 34 tPassed /= 1000; 35 printf("Time = %lld/n", tPassed); 36 return 1; 37 }
result:
1 [root@c0108 zlt]# g++ omp.c -o omp -fopenmp 2 [root@c0108 zlt]# ./omp 3 Time = 2859 4 Time = 718 5 [root@c0108 zlt]#
在四核的机器上,开了四个线程,加速比基本上是4,果然不错。
此外,BS一下clock()函数,我向来觉得它一无是处。。。
结果:
1 2 3 [root@c0108 zlt]# g++ omp.c -o omp -fopenmp 4 [root@c0108 zlt]# ./omp 5 Time = 2860000 6 Time = 2860000 7 [root@c0108 zlt]# 8
clock有三个问题:
1)如果超过一个小时,将要导致溢出.
2)函数clock没有考虑CPU被子进程使用的情况.
3)也不能区分用户空间和内核空间.
标签:
原文地址:http://www.cnblogs.com/hitwhhw09/p/4665444.html