我使用vs2012并想测试SSE和AVX的效率.SSE和AVX的代码几乎相同,只是SSE使用_m128而AVX使用_m256.我期望AVX代码比SSE代码快两倍,但测试结果显示它们的速度几乎相同.
我尝试选择/ arch:AVX或/ arch:SSE或/ NOT SET并注释SSE代码或注释AVX代码,无论我测试什么,SSE代码使用的时间约为2138ms,AVX代码约为2106ms.外部for循环仅用于增加循环时间,
#include "testfun.h"
#include <iostream>
#include <time.h>
#include <malloc.h>
#include "immintrin.h"
using namespace std;
#define dataLen 800000
void testfun()
{
float *buf1 = reinterpret_cast<float*>(_aligned_malloc( sizeof(float)*dataLen, 32 ));
float *buf2 = reinterpret_cast<float*>(_aligned_malloc( sizeof(float)*dataLen, 32 ));
for(int i=0; i<dataLen; i++)
{
buf1[i] = 1;
buf2[i] = 1;
}
double timePassed;
int t = clock();
float sum = 0;
//=========================SSE CODE=====================================
__m128 *p1 = (__m128 *)buf1;
__m128 *p2 = (__m128 *)buf2;
__m128 _result = _mm_set_ps1(0.0f);
for(int j=0;j<10000; j++) …Run Code Online (Sandbox Code Playgroud)