Fast intersection of sets: C++ vs C#

后端 未结 13 1691
野性不改
野性不改 2020-12-28 10:17

On my machine (Quad core, 8gb ram), running Vista x64 Business, with Visual Studio 2008 SP1, I am trying to intersect two sets of numbers very quickly.

I\'ve impleme

13条回答
  •  忘掉有多难
    2020-12-28 11:11

    Update:

    I modified the set_intersection code to use vectors, and to sort them (instead of using the sorted set class), and its MUCH faster now:

    Found the intersection of 319 values (using unordered_map) 1000 times, in 22187.5ms
    Found the intersection of 315 values (using set_intersection) 1000 times, in 2401.62ms
    

    Keep in mind: the larger set is created sorted, so sorting it might not take much time in this example.

    C++ Code:

    // MapPerformance.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    
    #include "timer.h"
    
    using namespace std;
    using namespace stdext;
    using namespace boost;
    
    int runIntersectionTest(vector set1, vector set2)
    {
        // hash_map theMap;
        // map theMap;
        unordered_map theMap;
    
        // Now intersect the two sets by populating the map
        for ( vector::iterator iterator = set1.begin(); iterator != set1.end(); iterator++ )
        {
            int value = *iterator;
    
            theMap[value] = 1;
        }
    
        int intersectionSize = 0;
    
        for ( vector::iterator iterator = set2.begin(); iterator != set2.end(); iterator++ )
        {
            int value = *iterator;
    
            unordered_map::iterator foundValue = theMap.find(value);
    
            if ( foundValue != theMap.end() )
            {
                theMap[value] = 2;
    
                intersectionSize++;
            }
        }
    
        return intersectionSize;
    
    }
    
    int runSetIntersection(vector set1, vector set2)
    {   
        sort(set1.begin(),set1.end());
        sort(set2.begin(),set2.end());
    
        set intersection;
    
        set_intersection(set1.begin(),set1.end(), set2.begin(), set2.end(), inserter(intersection, intersection.end()));
    
        return intersection.size(); 
    }
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        srand ( time(NULL) );
    
        vector set1;
        vector set2;
    
        set1.reserve(10000);
        set2.reserve(1000);
    
        // Create 100,000 values for set1
        for ( int i = 0; i < 100000; i++ )
        {
            int value = 1000000000 + i;
            set1.push_back(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.push_back(value);
        }
    
        int intersectionSize = 0;
    
    
        Timer timer;
        for ( int i = 0; i < 1000; i++ )
        {
            intersectionSize = runIntersectionTest(set1, set2);
        }
        timer.Stop();
    
        cout << "Found the intersection of " << intersectionSize << " values (using unordered_map) 1000 times, in " << timer.GetMilliseconds() << "ms" << endl;
    
        timer.Reset();
        for ( int i = 0; i < 1000; i++ )
        {
            intersectionSize = runSetIntersection(set1,set2);
        }
        timer.Stop();
    
        cout << "Found the intersection of " << intersectionSize << " values (using set_intersection) 1000 times, in " << timer.GetMilliseconds() << "ms" << endl;
    
        getchar();
    
        return 0;
    }
    

提交回复
热议问题