Calling C++ dll from .Net code is excruciatingly slow when a debugger is attached
I am using VS 2008 to compile and debug my code. In my application, the front end is C#, the backend is C++. I access the C++ code via interop.
One thing I notice is tha开发者_如何学编程t if I press F5, and I link in the debug version of the native code, it would take more than 20 seconds to run the native code. If I link in the release version of the native code, the time needed to run it is cut down to 7-8 seconds.
And if I don't have the VS 2008 debugger attached ( i.e., I click on the exe outside of the IDE), and I link in the release version of the C++ code, it takes only 1 second to run the C++ code.
Note that for all the testing, the input to C++ code is always always the same.
For sure, there are some overheads associated with the attachment of debugger, and debug build. But the difference in performance is just too big. Could there be anything I miss or I can tune up?
I find that this is very puzzling; C# in my experience doesn't introduce this much overhead, regardless of whether you are running with a debugger, or whether you are running in debug mode.
My best guess would be that loading the debugging symbols is what's slowing you down. I've seen the exact same thing happen with purely unmanaged C++ projects that are set to automatically load the symbol files for all of the Windows system libraries. Make sure that you're not automatically loading debugging symbols:
- In the "Tools" menu, click "Options".
- Expand the "Debugging" category to the left, and select the "Symbols" subcategory.
- Check the checkbox labeled "Search the above locations only when symbols are loaded manually".
Additionally, consider that mixed-mode debugging is extremely slow.
If you don't need the ability to step into the code in the C++ DLL, you can try turning off the "Enable unmanaged code debugging" option in your project's properties:
精彩评论