Visual C++:在发布模式下启动/不调试之间的区别
开始调试 (F5) 和启动但不调试 (CTRL-F5) 之间有什么区别代码何时以发布模式编译?
我发现对于某些 C++ 代码, CTRL-F5 比 F5 快 10 倍。如果我没记错的话,调试器附加到 F5 的执行进程,而不是 CTRL-F5 的执行进程。由于这是Release模式,编译后的代码没有任何调试信息。因此,如果我没有任何断点,两者的执行时间应该相同,不是吗?!
(假设“发布”和“调试”模式是创建新的 Visual C++ 项目时获得的典型配置。)
What is the difference between Start Debugging (F5) and Start without Debugging (CTRL-F5) when the code is compiled in Release mode?
I am seeing that CTRL-F5 is 10x faster than F5 for some C++ code. If I am not wrong, the debugger is attached to the executing process for F5 and it is not for CTRL-F5. Since this is Release mode, the compiled code does not have any debugging information. So, if I do not have any breakpoints, the execution times should be the same across the two, isn't it?!
(Assume that the Release and Debug modes are the typical configurations you get when you create a new Visual C++ project.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是,如果 Windows 检测到您的程序正在调试器下运行,则会放入一个特殊的调试堆中。这似乎发生在操作系统级别,并且与编译的任何调试/发布模式设置无关。
您可以通过设置环境变量来解决此“功能”: _NO_DEBUG_HEAP=1
这个同样的问题已经让我发疯了一段时间;今天我发现了以下内容,这篇文章就是从那里衍生出来的:
http://blogs.msdn .com/b/larryosterman/archive/2008/09/03/anatomy-of-a-heisenbug.aspx
The problem is that Windows drops in a special Debug Heap, if it detects that your program is running under a Debugger. This appears to happen at the OS level, and is independent of any Debug/Release mode settings for your compilation.
You can work around this 'feature' by setting an environment variable: _NO_DEBUG_HEAP=1
This same issue has been driving me nuts for a while; today I found the following, from whence this post is derived:
http://blogs.msdn.com/b/larryosterman/archive/2008/09/03/anatomy-of-a-heisenbug.aspx
“启动而不调试”只是告诉 Windows 像正常运行一样启动应用程序。
“从调试开始”启动 VS 调试器并让它在调试器中运行应用程序。
这实际上与调试/发布构建设置没有太大关系。
当您构建应用程序的默认“调试”配置时,您将与发布版本有以下主要区别:
这些项目中的每一项都是独立且可选的!您可以打开或关闭其中任何一项或全部,并且仍然在调试器下运行代码,您只是不会发现生活如此轻松。
当您运行“调试”时,由于以下几个原因,事情会表现不同:
"Start without debugging" just tells Windows to launch the app as it would normally run.
"Start with debugging" starts the VS debugger and has it run the app within the debugger.
This really doesn't have much to do with the debug/release build settings.
When you build the default 'debug' configuration of your app, you'll have the following main differences to the release build:
Each one of these items is independent and optional! You can turn any or all of them on or off and still run the code under the debugger, you just won't find life so easy.
When you run 'with debugging' things perform differently for several reasons:
IsDebuggerPresent()
和OutputDebugString()
< /a> 根据是否附加调试器而表现不同。IsDebuggerPresent()
只是返回另一个值,因此您的程序可以对此值做出反应并故意表现出不同的行为。当没有附加调试器时,OutputDebugString()
返回速度要快得多,因此,如果多次调用它,您会发现程序在没有调试器的情况下运行得更快。IsDebuggerPresent()
andOutputDebugString()
behave differently depending on whether a debugger is attached.IsDebuggerPresent()
simply returns another value, so your program can react to this value and behave differently on purpose.OutputDebugString()
returns much faster when there's no debugger attached, so if it's called lots of times you'll see that the program runs much faster without the debugger.当“带调试”运行时,即使对于发布模式,也会使用调试堆。这会导致使用大量 malloc/free 或 new/delete 的代码严重减慢,这可能在 C++ 代码中发生,而您却没有注意到,因为库/类往往向您隐藏这些内存管理内容。
When running 'with debugging' the debug heap is used, even for release mode. This causes severe slowdowns in code using a lot of malloc/free or new/delete, which can happen in C++ code without you noticing it because libraries/classes tend to hide this memory management stuff from you.