平時開發的時候,為了方便調試,visual studio 的Configuration 設置成Release。
同時為了事后調試,Optimization總是設置成Disabled。這樣做是方便查看變量的數值。
但遇到計算密集的功能實現,優化關閉還是挺費時間的。
void calc(int nMax)
{
int nTotal = 0;
for (int index = 0;index < nMax;index++)
{
nTotal = 0;
for (int subIndex = index;subIndex < nMax+index;subIndex++ )
{
nTotal += subIndex;
}
}
}
最初我的想法是project的優化關閉,相關文件的優化打開,測試后發現沒有什么作用。
參考visual studio的幫助后,發現可以針對函數進行優化。
這樣做考慮其他方法依舊可以事后調試。
在函數前后增加 #pragma optimize即可
#pragma optimize( "gs", on )
void calc(int nMax)
{
int nTotal = 0;
for (int index = 0;index < nMax;index++)
{
nTotal = 0;
for (int subIndex = index;subIndex < nMax+index;subIndex++ )
{
nTotal += subIndex;
}
}
}
#pragma optimize( "gs", off )
經過測試,針對函數的優化,性能和project優化相當。
未優化前:0.67秒
優化后:0.00秒
這樣以后事后調試還是很方便的。
測試環境:
ide:vs2010
項目:console
Configuration :Release。
Optimization:Disabled
實現代碼:
#include "stdafx.h"
#include <Windows.h>
#pragma optimize( "gs", on )
void calc(int nMax)
{
int nTotal = 0;
for (int index = 0;index < nMax;index++)
{
nTotal = 0;
for (int subIndex = index;subIndex < nMax+index;subIndex++ )
{
nTotal += subIndex;
}
}
}
#pragma optimize( "gs", off )
void retry(int nMin)
{
int nTry = 0;
nTry = nMin;
}
int _tmain(int argc, _TCHAR* argv[])
{
LARGE_INTEGER freq = {0};
LARGE_INTEGER beginPerformanceCount = {0};
LARGE_INTEGER closePerformanceCount = {0};
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&beginPerformanceCount);
calc(10000);
QueryPerformanceCounter(&closePerformanceCount);
retry(2020);
double delta_seconds = (double)(closePerformanceCount.QuadPart - beginPerformanceCount.QuadPart) / freq.QuadPart;
printf("%f",delta_seconds);
getchar();
return 0;
}
相關鏈接:
https://msdn.microsoft.com/en-us/library/chh3fb0k(v=vs.100).aspx

