【问题标题】:Why does my STL code run so slowly when I have the debugger/IDE attached?为什么当我连接了调试器/IDE 时我的 STL 代码运行如此缓慢?
【发布时间】:2009-06-29 20:29:06
【问题描述】:

我正在使用 Visual Studio 2008 SP1 在 Windows Vista Business x64、四核机器、8gb 内存上运行以下代码。

如果我构建一个发布版本,并从命令行运行它,它会报告 31 毫秒。如果我然后从 IDE 启动它,使用 F5,它会报告 23353 毫秒。

以下是时间:(所有 Win32 版本)

  • 调试,命令行:421ms
  • 调试,来自 IDE:24,570 毫秒
  • 发布,命令行:31 毫秒
  • 发布,来自 IDE:23,353 毫秒

代码:

#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 << " milliseconds\n";
}

【问题讨论】:

  • 您可能想查看有关 Markdown 的帮助,以便更好地格式化代码
  • 是的,老实说,我发现它真的很难合作。 :) 我点击了“代码”按钮并粘贴了我的代码,它真的毁了它。
  • 先粘贴代码,然后全选,点击代码按钮。 :)
  • 它并不像你说的那么简单。 :)
  • 这个问题其实不用代码就可以解决的:)但前提是你先体验过。

标签: c++ visual-studio performance ide stl


【解决方案1】:

默认情况下,在 Microsoft 调试器(windbg、kd、cdb、Visual Studio 调试器)下运行会强制 Windows 使用调试堆而不是默认堆。在 Windows 2000 及更高版本上,默认堆是Low Fragmentation Heap,与调试堆相比,这非常好。您可以使用HeapQueryInformation 查询您正在使用的堆类型。

要解决您的特定问题,您可以使用此知识库文章中推荐的众多选项之一:Why the low fragmentation heap (LFH) mechanism may be disabled on some computers that are running Windows Server 2003, Windows XP, or Windows 2000

对于 Visual Studio,我更喜欢将 _NO_DEBUG_HEAP=1 添加到 Project Properties-&gt;Configuration Properties-&gt;Debugging-&gt;Environment。这对我来说总是有用的。

【讨论】:

  • 谢谢,成功了。一旦我设置了 _NO_DEBUG_HEAP=1 ,那么代码在附加调试器的情况下运行速度与没有附加调试器一样快。我想我会失去一些保护/问题检测。
  • 你失去了很多可调试性。
【解决方案2】:

在 VS IDE 中按暂停显示额外的时间似乎花费在 malloc/free 中。如果附加了调试器,这将使我相信 MS 的 malloc 和免费实现中的调试支持具有额外的逻辑。这可以解释控制台和调试器的时间差异。

编辑:通过使用 CTRL+F5 v. F5 运行确认(我的机器上为 1047ms v. 9088ms)

【讨论】:

  • 我也注意到了,中断通常会在 malloc 中显示代码。
【解决方案3】:

所以听起来这可能只是在附加调试器时发生的事情。但是,我无法理解性能从 30 毫秒变为 23,000 毫秒的原因,尤其是当我的其余代码似乎运行速度一样快时,无论是否附加了调试器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 2013-01-03
    • 1970-01-01
    • 2016-03-27
    • 2019-04-14
    • 1970-01-01
    相关资源
    最近更新 更多