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

2022-01-20 00:00:00 visual-studio performance ide c++ stl

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

I'm running the following code, using Visual Studio 2008 SP1, on Windows Vista Business x64, quad core machine, 8gb ram.

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

If I build a release build, and run it from the command line, it reports 31ms. If I then start it from the IDE, using F5, it reports 23353ms.

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

Here are the times: (all Win32 builds)

  • 调试,命令行:421ms
  • 调试,来自 IDE:24,570 毫秒
  • RELEASE,命令行:31ms
  • 发布,来自 IDE:23,353 毫秒

代码:

#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
";
}

推荐答案

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

Running under a Microsoft debugger (windbg, kd, cdb, Visual Studio Debugger) by default forces Windows to use the debug heap instead of the default heap. On Windows 2000 and above, the default heap is the Low Fragmentation Heap, which is insanely good compared to the debug heap. You can query the kind of heap you are using with HeapQueryInformation.

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

To solve your particular problem, you can use one of the many options recommended in this KB article: Why the low fragmentation heap (LFH) mechanism may be disabled on some computers that are running Windows Server 2003, Windows XP, or Windows 2000

对于 Visual Studio,我更喜欢将 _NO_DEBUG_HEAP=1 添加到 Project Properties->Configuration Properties->Debugging->Environment.这对我来说总是有用的.

For Visual Studio, I prefer adding _NO_DEBUG_HEAP=1 to Project Properties->Configuration Properties->Debugging->Environment. That always does the trick for me.

相关文章