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;
}


以上为代码。。。希望能有知道的帅哥做出解答。。。谢谢了!