Why does my STL code run so slowly when I have the debugger/IDE attached?

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

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.

Here are the times: (all Win32 builds)

  • DEBUG, command line: 421ms
  • DEBUG, from the IDE: 24,570ms
  • RELEASE, command line: 31ms
  • RELEASE, from IDE: 23,353ms
  • code:

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

    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.

    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

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


    Pressing pause while in the VS IDE shows that the additional time appears to be spent in malloc/free. This would lead me to believe the debugging support in MS's malloc and free implementation have additional logic if the debugger is attached. This would explain the discrepancy in times from the console and from the debugger.

    EDIT: Confirmed by running with CTRL+F5 v. F5 (1047ms v. 9088ms on my machine)


    So it sounds like this may just be what happens when one attaches the debugger. However, I just can't my head around the performance changing from 30ms to 23,000ms because of that, especially when the rest of my code seems to run just as fast whether or not the debugger is attached.

    链接地址: http://www.djcxy.com/p/82286.html

    上一篇: 寻找上次释放内存的位置?

    下一篇: 为什么我的调试器/ IDE连接时,我的STL代码运行速度太慢?