To: 很土:我確實沒有搜到任何相關的信息,所以還請你指點。謝謝。
我昨天在寫函數然后測試優化選項的時候,非常奇怪的發現,如果打開了
運行時檢測 選項中的 堆棧幀檢測 ,代碼性能就會瘋狂的攀升一個數量級
然而從理論上來說,由于堆棧幀檢測添加了Check ESP的CRT函數調用(實際的匯編也是如此),性能應該略有下降才是,但是我不知道為什么它的性能居然能極大幅度的提高。
#include "stdafx.h"
#include <math.h>
#include <float.h>
#include <emmintrin.h>
#include <windows.h>
#include <d3dxmath.h>

using namespace std;


struct __declspec(align(16)) Matrix
{
float m[4][4];
};

class Profiler


{
public:
LARGE_INTEGER s;
LARGE_INTEGER e;


__int64 Begin()
{
QueryPerformanceCounter(&s);

return s.QuadPart;
}


__int64 End()
{
QueryPerformanceCounter(&e);

return e.QuadPart - s.QuadPart;
}
};

int Multiply(float o[][4], const float a[][4], const float b[][4], int i)


{
o[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0] + a[0][2] * b[2][0] + a[0][3] * b[3][0];
o[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1] + a[0][2] * b[2][1] + a[0][3] * b[3][1];
o[0][2] = a[0][0] * b[0][2] + a[0][1] * b[1][2] + a[0][2] * b[2][2] + a[0][3] * b[3][2];
o[0][3] = a[0][0] * b[0][3] + a[0][1] * b[1][3] + a[0][2] * b[2][3] + a[0][3] * b[3][3];

o[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0] + a[1][2] * b[2][0] + a[1][3] * b[3][0];
o[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1] + a[1][2] * b[2][1] + a[1][3] * b[3][1];
o[1][2] = a[1][0] * b[0][2] + a[1][1] * b[1][2] + a[1][2] * b[2][2] + a[1][3] * b[3][2];
o[1][3] = a[1][0] * b[0][3] + a[1][1] * b[1][3] + a[1][2] * b[2][3] + a[1][3] * b[3][3];

o[2][0] = a[2][0] * b[0][0] + a[2][1] * b[1][0] + a[2][2] * b[2][0] + a[2][3] * b[3][0];
o[2][1] = a[2][0] * b[0][1] + a[2][1] * b[1][1] + a[2][2] * b[2][1] + a[2][3] * b[3][1];
o[2][2] = a[2][0] * b[0][2] + a[2][1] * b[1][2] + a[2][2] * b[2][2] + a[2][3] * b[3][2];
o[2][3] = a[2][0] * b[0][3] + a[2][1] * b[1][3] + a[2][2] * b[2][3] + a[2][3] * b[3][3];

o[3][0] = a[3][0] * b[0][0] + a[3][1] * b[1][0] + a[3][2] * b[2][0] + a[3][3] * b[3][0];
o[3][1] = a[3][0] * b[0][1] + a[3][1] * b[1][1] + a[3][2] * b[2][1] + a[3][3] * b[3][1];
o[3][2] = a[3][0] * b[0][2] + a[3][1] * b[1][2] + a[3][2] * b[2][2] + a[3][3] * b[3][2];
o[3][3] = a[3][0] * b[0][3] + a[3][1] * b[1][3] + a[3][2] * b[2][3] + a[3][3] * b[3][3];

return i / 1000;
}

int _tmain(int argc, _TCHAR* argv[])


{
__declspec(align(16)) D3DXVECTOR3 v(2.0f,2.0f,2.0f);
HANDLE hp = GetCurrentProcess();
HANDLE ht = GetCurrentThread();

SetPriorityClass(hp, REALTIME_PRIORITY_CLASS);
SetThreadPriority(ht, THREAD_PRIORITY_TIME_CRITICAL);

Profiler p;
Matrix m, m1, m2;
memset(m.m, 0, 16);
memset(m1.m, 0, 16);

m.m[0][0] = 1.0f;
m.m[0][1] = 2.0f;
m.m[0][2] = 3.0f;
m.m[0][3] = 4.0f;

m.m[1][0] = 5.0f;
m.m[1][1] = 6.0f;
m.m[1][2] = 7.0f;
m.m[1][3] = 8.0f;

int y = 0;
int t = 0;

p.Begin();
for(int i = 0; i < 100000; ++i)


{
t = Multiply(m2.m, m.m, m1.m, i);
y += t;
}
__int64 x = p.End();

cout << x << " MatrixMultiply C"<< y << endl;

SetPriorityClass(hp, NORMAL_PRIORITY_CLASS);
SetThreadPriority(ht, THREAD_PRIORITY_NORMAL);

system("pause");

return 0;
}


以上為代碼。。。希望能有知道的帥哥做出解答。。。謝謝了!