【问题标题】:why is set_intersection in STL so slow?为什么 STL 中的 set_intersection 这么慢?
【发布时间】:2009-06-29 16:54:17
【问题描述】:

我在 STL 中使用 set_intersection 将一组 100,000 个数字和一组 1,000 个数字相交,它需要 21 秒,而在 C# 中需要 11 毫秒。

C++ 代码:

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(); 
}

C#代码:

static int runIntersectionTest()
    {
        Random random = new Random(DateTime.Now.Millisecond);

        Dictionary<int,int> theMap = new Dictionary<int,int>();

        List<int> set1 = new List<int>();
        List<int> set2 = new List<int>();

            // Create 100,000 values for set1
            for ( int i = 0; i < 100000; i++ )
            {
                int value = 1000000000 + i;
                set1.Add(value);
            }

            // Create 1,000 values for set2
            for ( int i = 0; i < 1000; i++ )
            {
                int value = 1000000000 + (random.Next() % 200000 + 1);
                set2.Add(value);
            }

            // Now intersect the two sets by populating the map
        foreach( int value in set1 )
            {
                theMap[value] = 1;
            }

            int intersectionSize = 0;

        foreach ( int value in set2 )
        {
            int count;
            if ( theMap.TryGetValue(value, out count ) )
            {
                intersectionSize++;
                theMap[value] = 2;
            }
            }

            return intersectionSize;
    }
}

【问题讨论】:

  • 这不是一个有用的评论:你是在计时整个程序,还是只是 set_intersection() 调用?
  • 您是否正在计时初始集合的创建以及交叉点的创建时间?
  • 您确实意识到 C++ std::set 是一个基于树的结构,而 C# Dictionary 是一个基于数组的哈希表,而 List 只是一个数组,对吧?在考虑代码的分配问题之前,您已经在比较苹果和橙子了。
  • 除了以上问题。在 C# 示例中,您没有构建集合的交集(只是获取大小)。您需要进行第三次传递并从“theMap”中删除仅在 set1 中的所有成员。
  • 你如何编译这个?其他人似乎无法重现您的时间安排。

标签: c# c++ performance stl intersection


【解决方案1】:

有几件事会让你的两个例子更具可比性。

首先,您在 STL 中的示例不太正确,一方面,两个集合都应该按升序排序(在 STL 中说“严格的弱排序”)。

其次,您使用的“集合”在 STL 中实现为树,而“列表”则是链表。随机插入集合比插入列表末尾更昂贵。

尝试在 C++ 示例中使用整数列表并先对列表进行排序(否则设置 inersection 将无法正常工作),我认为您会看到更有利的结果。

【讨论】:

    【解决方案2】:

    我在我的 linux 机器上运行了你的 C++ 代码

    $ time ./test
    
    real    0m0.073s
    user    0m0.060s
    sys     0m0.003s
    

    21s 对我来说意味着你编译时没有优化。如果您使用 MSVC,请确保您已列出 编译定义中的_SECURE_SCL=0(参见msdn)。否则,所有 STL 迭代器操作都非常缓慢。

    【讨论】:

    • +1 表示 _SCL_SECURE。我以前没有听说过这面旗帜。你知道它是否在发布版本中被禁用了吗?
    • 这是 boost 邮件列表中的讨论主题。他们说 _SCL_SECURE 默认启用(设置为 1),即使在发布模式下也是如此。
    • 它在调试和发布版本中默认启用。根据今年 BoostCon 的介绍,它将在 VS2010 的发布版本中禁用。 :)
    • 哎呀,它看起来像是 _SECURE_SCL,而不是 _SCL_SECURE :)
    • 嗯,MSDN 同意你的观点,jalf。已更正。
    【解决方案3】:

    在这个古老的 3GHz Pentium 4 上,在禁用优化的调试版本中,整个 runIntersectionTestAlgo 函数需要 2734 毫秒。我用VS2008 SP1编译。

    如果我启用优化,我会得到 93 毫秒。

    这是我的代码:

    #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(); 
    }
    
    #include <windows.h>
    #include <iostream>
    
    int main(){
        DWORD start = GetTickCount();
    
        runIntersectionTestAlgo();
    
        DWORD span = GetTickCount() - start;
    
        std::cout << span << " milliseconds\n";
    }
    

    禁用 _SECURE_SCL 对发布版本没有任何影响,它仍然徘徊在 100 毫秒左右。

    GetTickCount 当然并不理想,但它应该足以区分 21 秒和不到 100 毫秒。

    所以我断定你的基准有问题。

    【讨论】:

    • 哇。我拿了你的代码,运行它,它在 31 毫秒内运行。然后我在附加调试器的情况下运行它(就像我为测试所做的那样),它在 23353 毫秒内运行。
    • STL 使用了许多需要内联的中间函数才能使性能可以接受。 GCC 至少可以让您同时启用调试和优化,并且最近的 GDB 版本可以显示和逐步执行内联函数调用,因此您可以以可以承受的速度调试这些东西;)您可以尝试在没有其他优化的情况下打开内联,看看是什么样的你得到的速度。
    【解决方案4】:

    我更新了您的示例以使用我在单元测试时使用的一些计时器代码。在我的机器上,我得到以下时间(基于 -O3):

    First loop 0.0040654
    Second loop 4.8e-05
    Intersection 0.000349
    Intersection size: 50
    

    基于此,如果我正确读取小数点,将项目插入第一组需要 4 毫秒,将项目插入第二组需要 50 微秒,执行需要 1/3 毫秒路口。

    我无法在我的机器上运行你的 C# 示例,所以我无法比较时间,但绝对不是你发布的 21 秒。

    【讨论】:

      【解决方案5】:

      您的 C# 和 C++ 代码的工作方式不同。 C# 代码使用神奇的散列技巧来提高速度,您的 C++ 代码使用树技巧来提高速度。可能会加快速度的一件事(忽略您的测试似乎被破坏的事实)是使用散列,如下所示:

      1. 创建两个集合之一的hash_map
      2. 遍历第二个集合中的每个元素。如果 `hash_map1 包含该元素,请将其添加到您的结果中。

      【讨论】:

      • 是的,它们是不同的。我之前使用 std:map、stdext:hash_map 和 boost::unordered_set 在 C++ 中尝试了 C# 方法,并得到了同样糟糕的结果。我确定如果我将 C# 代码更改为使用 HashSet 和 .IntersectWith 它会一样快(或者可能更快)。
      猜你喜欢
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2016-09-28
      • 2020-02-08
      • 2012-07-17
      • 2011-11-07
      • 2015-08-24
      • 2013-08-06
      相关资源
      最近更新 更多