¿Contenedor rápido de C ++ como C # HashSet <T> y Dictionary <K, V>?

He usado HashSet y Dictionary mucho en C #, y los encontré muy rápido ...

He intentado usar std :: map y std :: hash_map y los encuentro muy lentos en comparación. ¿Suena esto como el comportamiento esperado? ¿Hay algo que pueda estar haciendo mal en mi uso de std :: hash_map?

O, ¿hay un mejor contenedor de Hash en C ++?

Soy hash int32s, generalmente alrededor de 100,000 de ellos.

Actualización: he creado una reproducción en C # y C ++. Ejecuta dos pruebas, toman 19 ms y 13 ms en C #, y aproximadamente 11,000 ms en C ++. Debe haber algo realmente mal con mi código de C ++ :)

(Ambos se ejecutaron como versiones de lanzamiento, ambas son aplicaciones de consola)

Salida C #:

<code>Found 511 values in the intersection, in 19 ms
Found 508 values in the intersection, in 13 ms
</code>

Salida de C ++:

<code>Found 308 values in the intersection, in 11764.7ms
Found 316 values in the intersection, in 11742.8ms
</code>

Salida de C ++ (usando stdext :: hash_map en lugar de std :: map)

<code>Found 300 values in the intersection, in 383.552ms
Found 306 values in the intersection, in 2277.02ms
</code>

Salida de C ++ (usando stdext :: hash_map, una versión x64 construida)

<code>Found 292 values in the intersection, in 1037.67ms
Found 302 values in the intersection, in 3663.71ms
</code>

Notas:

Set2 no está siendo poblado como quería en C ++, esperaba que tuviera una intersección del 50% con Set1 (como lo hace en C #), pero tuve que multiplicar mi número al azar por 10 por alguna razón para incluso lograr que parcialmente no se intersecta

DO#:

<code>    static void Main(string[] args)
    {
        int start = DateTime.Now.Millisecond;
        int intersectionSize = runIntersectionTest();
        int duration = DateTime.Now.Millisecond - start;

        Console.WriteLine(String.Format("Found {0} values in the intersection, in {1} ms", intersectionSize, duration));

        start = DateTime.Now.Millisecond;
        intersectionSize = runIntersectionTest();
        duration = DateTime.Now.Millisecond - start;

        Console.WriteLine(String.Format("Found {0} values in the intersection, in {1} ms", intersectionSize, duration));

        Console.ReadKey();
    }

    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;
    }
</code>

C ++:

<code>int runIntersectionTest()
{
    std::map<int,int> theMap;

    vector<int> set1;
    vector<int> set2;

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

    // Now intersect the two sets by populating the map
    for ( vector<int>::iterator iterator = set1.begin(); iterator != set1.end(); iterator++ )
    {
        int value = *iterator;

        theMap[value] = 1;
    }

    int intersectionSize = 0;

    for ( vector<int>::iterator iterator = set2.begin(); iterator != set2.end(); iterator++ )
    {
        int value = *iterator;

        map<int,int>::iterator foundValue = theMap.find(value);

        if ( foundValue != theMap.end() )
        {
            theMap[value] = 2;

            intersectionSize++;
        }
    }

    return intersectionSize;

}

int _tmain(int argc, _TCHAR* argv[])
{
    srand ( time(NULL) );

    Timer timer;
    int intersectionSize = runIntersectionTest();
    timer.Stop();

    cout << "Found " << intersectionSize << " values in the intersection, in " << timer.GetMilliseconds() << "ms" << endl;

    timer.Reset();
    intersectionSize = runIntersectionTest();
    timer.Stop();

    cout << "Found " << intersectionSize << " values in the intersection, in " << timer.GetMilliseconds() << "ms" << endl;

    getchar();

    return 0;
}
</code>

Respuestas a la pregunta(6)

Su respuesta a la pregunta