现在的位置: 首页 > 综合 > 正文

在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试

2013年10月09日 ⁄ 综合 ⁄ 共 1166字 ⁄ 字号 评论关闭

转自 http://blog.csdn.net/gengshenghong/article/details/7011373

下面是一个简单的测试SSE指令性能的程序,可以看到明显的性能提升。

(说明:程序中的timing.h使用的是http://blog.csdn.net/gengshenghong/article/details/6973086中介绍的时间间隔获取方法)

  1. #define WIN  
  2. #include "timing.h"  
  3. #include <intrin.h>  
  4. #include <stdlib.h>  
  5. #include <math.h>  
  6.   
  7. #define N 4*100000      // 注意:必须是4的倍数,否则使用SSE指令计算,要进行一些处理,从而保证正确。  
  8. _MM_ALIGN16 float op1[N];  
  9. _MM_ALIGN16 float op2[N];  
  10. _MM_ALIGN16 float result1[N];  
  11. _MM_ALIGN16 float result2[N];  
  12.   
  13. void init()  
  14. {  
  15.     for(int i = 0;i < N; i++)  
  16.     {  
  17.         op1[i] = (float)rand()/(float)RAND_MAX;  
  18.         op2[i] = (float)rand()/(float)RAND_MAX;  
  19.     }  
  20. }  
  21.   
  22. void checkResult(int debug)  
  23. {  
  24.     bool isSame = true;  
  25.     for(int i = 0;i < N; i++)  
  26.     {  
  27.         if (debug)  
  28.         {  
  29.             printf("%lf     %lf\n", result1[i], result2[i]);  
  30.         }  
  31.         else  
  32.         {  
  33.             if (fabs(result1[i] - result2[i]) > 0.000001)  
  34.             {  
  35.                 isSame = false;  
  36.                 break;  
  37.             }  
  38.         }  
  39.     }  
  40.     if (!debug) {  
  41.         if (isSame)  
  42.             printf("Result is Same\n");  
  43.         else  
  44.             printf("Result is not same\n");  
  45.     }  

抱歉!评论已关闭.