您的位置:首页 > 其它

vs2010 在函数级别设置优化

2015-06-26 16:36 267 查看
平时开发的时候,为了方便调试,visualstudio的Configuration设置成Release。

同时为了事后调试,Optimization总是设置成Disabled。这样做是方便查看变量的数值。

但遇到计算密集的功能实现,优化关闭还是挺费时间的。

voidcalc(intnMax) { intnTotal=0; for(intindex=0;index<nMax;index++) { nTotal=0; for(intsubIndex=index;subIndex<nMax+index;subIndex++) { nTotal+=subIndex; } } }

  


最初我的想法是project的优化关闭,相关文件的优化打开,测试后发现没有什么作用。





参考visualstudio的帮助后,发现可以针对函数进行优化。

这样做考虑其他方法依旧可以事后调试。

在函数前后增加#pragmaoptimize即可

#pragmaoptimize("gs",on)
voidcalc(intnMax) { intnTotal=0; for(intindex=0;index<nMax;index++) { nTotal=0; for(intsubIndex=index;subIndex<nMax+index;subIndex++) { nTotal+=subIndex; } } }#pragmaoptimize("gs",off)


  

经过测试,针对函数的优化,性能和project优化相当。

未优化前:0.67秒

优化后:0.00秒

这样以后事后调试还是很方便的。

测试环境:

ide:vs2010

项目:console

Configuration:Release。

Optimization:Disabled

实现代码:

#include"stdafx.h"
#include<Windows.h>

#pragmaoptimize("gs",on)
voidcalc(intnMax) { intnTotal=0; for(intindex=0;index<nMax;index++) { nTotal=0; for(intsubIndex=index;subIndex<nMax+index;subIndex++) { nTotal+=subIndex; } } }#pragmaoptimize("gs",off)

voidretry(intnMin)
{
intnTry=0;
nTry=nMin;
}

int_tmain(intargc,_TCHAR*argv[])
{
LARGE_INTEGER freq ={0};
LARGE_INTEGER beginPerformanceCount ={0};
LARGE_INTEGER closePerformanceCount ={0};

QueryPerformanceFrequency(&freq);

QueryPerformanceCounter(&beginPerformanceCount);
calc(10000);
QueryPerformanceCounter(&closePerformanceCount);

retry(2020);
doubledelta_seconds=(double)(closePerformanceCount.QuadPart-beginPerformanceCount.QuadPart)/freq.QuadPart;
printf("%f",delta_seconds);
getchar();
return0;
}


  

相关链接:

https://msdn.microsoft.com/en-us/library/chh3fb0k(v=vs.100).aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: