The attached, trivial, test program tests the performance of emptying a simple std::map. Using MSVC 2008 and 2010, the debug build will take <30seconds when executed from a command prompt but almost 3 minutes when executed from within the debugger. The call to clear() is entirely responsible for the difference. If I break into the debugger the callstack will always point to HeapFree.
Question: Why the huge difference? Can I somehow change debug heap settings so it’ll be fast when executed in the debugger?
#include <map>
int
main ( int, char )
{
std::map< time_t, double > test;
for ( int i = 0; i < 1000000; ++i )
{
test[i] = i / 3.14;
}
test.clear();
return 0;
}
Trying setting the environment variable _NO_DEBUG_HEAP=1 in the program’s initial environment. This disables Windows’ internal debug heap, which might make it harder to debug memory corruption problems.
This KB article mentions the flag, and you can infer that the default (low-fragmentation heap) is disabled if the program is run in a debugger without that environment variable. See also this blog post, which discusses how the debug heap can slow down their program by 3-5 times.