在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
来源:互联网 发布:天地图数据配图标准 编辑:程序博客网 时间:2024/06/06 04:19
下面是一个简单的测试SSE指令性能的程序,可以看到明显的性能提升。
(说明:程序中的timing.h使用的是http://blog.csdn.net/gengshenghong/article/details/6973086中介绍的时间间隔获取方法)
- #define WIN
- #include "timing.h"
- #include <intrin.h>
- #include <stdlib.h>
- #include <math.h>
- #define N 4*100000 // 注意:必须是4的倍数,否则使用SSE指令计算,要进行一些处理,从而保证正确。
- _MM_ALIGN16 float op1[N];
- _MM_ALIGN16 float op2[N];
- _MM_ALIGN16 float result1[N];
- _MM_ALIGN16 float result2[N];
- void init()
- {
- for(int i = 0;i < N; i++)
- {
- op1[i] = (float)rand()/(float)RAND_MAX;
- op2[i] = (float)rand()/(float)RAND_MAX;
- }
- }
- void checkResult(int debug)
- {
- bool isSame = true;
- for(int i = 0;i < N; i++)
- {
- if (debug)
- {
- printf("%lf %lf\n", result1[i], result2[i]);
- }
- else
- {
- if (fabs(result1[i] - result2[i]) > 0.000001)
- {
- isSame = false;
- break;
- }
- }
- }
- if (!debug) {
- if (isSame)
- printf("Result is Same\n");
- else
- printf("Result is not same\n");
- }
- }
- void add1()
- {
- for(int i = 0; i < N;i++)
- result1[i] = op1[i] + op2[i];
- }
- void add2()
- {
- __m128 a;
- __m128 b;
- __m128 c;
- for(int i = 0; i < N;i = i + 4)
- {
- // Load
- a = _mm_load_ps(op1 + i);
- b = _mm_load_ps(op2 + i);
- c = _mm_add_ps(a, b); // c = a + b
- _mm_store_ps(result2 + i, c);
- }
- }
- int main(int argc, char* argv[])
- {
- init();
- srand((unsigned int)time(NULL));
- printf("Add a vector:\n");
- startTiming();
- add1();
- stopWithPrintTiming();
- printf("\n");
- printf("Add a vector with SSE instructions:\n");
- startTiming();
- add2();
- stopWithPrintTiming();
- printf("\n");
- checkResult(0);
- return 0;
- }
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用
- 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用
- 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用
- 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用
- 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用
- 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用
- 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用
- 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用
- 在C/C++代码中使用SSE等指令集的指令(3)SSE指令集基础
- 在C/C++代码中使用SSE等指令集的指令(3)SSE指令集基础
- 在C/C++代码中使用SSE等指令集的指令(3)SSE指令集基础
- J - 病毒
- 移动搜索应着手独立发展还是沦为二级功能
- poj 2154 Color(欧拉函数模板+ploya定理)
- 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用
- 如何检测内存泄漏
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- Android 容易定制OptionMenu外观
- 读书笔记 重来:更为简单有效的商业思维1
- STL版本介绍:HP STL、SGI STL、STL Port、PJ STL、RW STL
- php 字符串与2进制的转换
- Session 研究
- Android使用Jsoup解析HTML页面
- Windows 内存挂认识
- C#下获取文件的MD5值