views:

119

answers:

3

I have a native c++ program which runs > 20 times slower when started with Debug (F5) but runs at normal speed when using start without debug (Ctrl+F5).

Is there some setting I did choose wrong or something?

[Update] It does not matter whether I use a debug or release build. Also if I use Windbg the program is a magnitude slower. [/Update]

A: 

Debugging Visual C++ comes with lots and lots of overhead, especially in the STL. Try not defining _DEBUG, and define NDEBUG.

Matt Joiner
That doesn't answer the OP's question though -- it's still a debug build, just running outside the IDE. The STL diagnostics will still run.
the_mandrill
A: 

There are several things that are different if you run the debug build outside the IDE. One is that the IDE takes a while to load symbols, and if you depend on a lot of libraries then that startup time can be significant. If you are using a symbol server (including the microsoft public symbol server) then this may add to the startup time, so ensure you have a local symbol cache in your _NT_SYMBOL_PATH variable if that is the case.

Also the IDE runs with the debug heap enabled, but I don't think this happens if you run outside the IDE.

the_mandrill
I am using the symbol server. But I am not speaking about the startup time but the time the program uses to actually run. I have diagnostic about with std::cout, which has about one per second with Ctrl+F5 and one evvery 30s or so when runing with F5.I think the heap debbuging depends on the _DEBUG macro which will do heap checking in both cases.
plaisthos
Do you have any conditional breakpoints or tracepoints set? These can slow execution down by an order of magnitude. Try disabling all breakpoints temporarily.
the_mandrill
Unfortunally I do not have any such. Would have been too easy :)
plaisthos
What about any addins? Another thing to try is running Process Monitor to see if the devenv.exe process is accessing any files. I've found some cases where this happens a lot -- devenv keeps accessing my header files. Also try deleting the .suo file.
the_mandrill
+1  A: 

This is of course not caused by having the _DEBUG symbol defined or compiling the code in the debug configuration. The added debugging code runs whether or not the debugger is attached to the program.

The debugger doesn't normally affect code execution, it stays out of the way by calling WaitForDebugEvent. Which blocks it, until the operating system tells it that something noteworthy happened. That can trigger a bunch of code in the debugger that can slow down your program. You can see the events listed in the DEBUG_EVENT structure documentation.

Annotating them a bit beyond the documentation: the debugger steps in and can slow down your program when:

  • The program loads or unloads a DLL. Lots of stuff happens during load, the debugger goes hunting for a debug symbol file (.pdb). It may contact a symbol server to download it. Any breakpoints that were set in the DLL source code will get activated. This can be quite slow, but the effect is temporary and generally only slows down the startup. You can see the load/unload notification in the Output window.

  • The program raises an exception. This activates the debugger at the moment the exception is raised, a "first chance notification". Which can be very helpful, you can use the Debug + Exception, Thrown checkbox to make the debugger stop when the exception is raised. You can see the notification message in the Output window. This does slow down code that raises and catches exceptions tremendously and is quite likely the source of your slowdown. Never use exceptions for flow control.

  • A thread starts running or terminates. Again, a notification message is printed to the Output window. You'd have to create a lot of threads to make this slow down your program.

  • When your program uses OutputDebugString() for tracing purposes. Visible in the Output window. Another good candidate for a slow down, output falls in the bit bucket if no debugger is attached. You shouldn't have any trouble diagnosing this as the cause, the obvious side-effect is seeing a lot of messages in the Output window.

  • When the program hits a breakpoint. Not a lot of reasons to be stumped by that one. But you can set breakpoints that slow down the program a lot yet don't cause a debugger break. Particularly the Conditional breakpoint, Hit counter, Filter and the When Hit operation will be slow. Use Debug + Windows + Breakpoints to review the breakpoints that are defined.

Hans Passant