当我连接调试器/ IDE时,为什么我的STL代码运行得如此之慢?

Ale*_*ack 4 c++ ide performance stl visual-studio

我在Windows Vista Business x64,四核机器,8gb ram上使用Visual Studio 2008 SP1运行以下代码.

如果我构建一个发布版本,并从命令行运行它,它报告31ms.如果我然后从IDE启动它,使用F5,它报告23353ms.

以下是时间:(所有Win32版本)

  • DEBUG,命令行:421ms
  • 来自IDE的DEBUG:24,570ms
  • RELEASE,命令行:31ms
  • 发布,来自IDE:23,353ms

码:

#include <windows.h>
#include <iostream>

#include <set>
#include <algorithm>
using namespace std;

int runIntersectionTestAlgo()
{   

    set<int> set1;
    set<int> set2;
    set<int> intersection;


    // Create 100,000 values for set1
    for ( int i = 0; i < 100000; i++ )
    {
        int value = 1000000000 + i;
        set1.insert(value);
    }

    // Create 1,000 values for set2
    for ( int i = 0; i < 1000; i++ )
    {
        int random = rand() % 200000 + 1;
        random *= 10;

        int value = 1000000000 + random;
        set2.insert(value);
    }

    set_intersection(set1.begin(),set1.end(), set2.begin(), set2.end(), inserter(intersection, intersection.end()));

    return intersection.size(); 
}

int main(){
    DWORD start = GetTickCount();

    runIntersectionTestAlgo();

    DWORD span = GetTickCount() - start;

    std::cout << span << " milliseconds\n";
}
Run Code Online (Sandbox Code Playgroud)

MSN*_*MSN 9

默认情况下,在Microsoft调试器(windbg,kd,cdb,Visual Studio调试器)下运行会强制Windows使用调试堆而不是默认堆.在Windows 2000及更高版本中,默认堆是低碎片堆,与调试堆相比,这是非常好的.您可以使用HeapQueryInformation查询正在使用的堆类型.

要解决您的特定问题,您可以使用此知识库文章中推荐的众多选项之一:为什么在运行Windows Server 2003,Windows XP或Windows 2000的某些计算机上可能禁用低碎片堆(LFH)机制

对于Visual Studio,我更喜欢加入_NO_DEBUG_HEAP=1Project Properties->Configuration Properties->Debugging->Environment.这总是对我有用.