vs2010 在函数级别设置优化

平时开发的时候,为了方便调试,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的优化关闭,相关文件的优化打开,测试后发现没有什么作用。

Untitled

参考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

posted on 2015-06-26 16:36  febwave  阅读(2867)  评论(0编辑  收藏  举报

导航