平時開發的時候,爲了方便調試,visual studio 的Configuration 設置成Release。ide
同時爲了過後調試,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便可orm
#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優化至關。blog
未優化前:0.67秒開發
優化後:0.00秒get
這樣之後過後調試仍是很方便的。
測試環境:
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