Ho usato HashSet e Dizionario molto in C#, e li ho trovati molto veloce ...Contenitore C++ veloce come C# HashSet <T> e Dizionario <K,V>?
Ho provato con std :: map e std :: hash_map e li sto trovando molto lenta in confronto. Questo suona come un comportamento previsto? C'è qualcosa che potrei sbagliare nel mio uso di std :: hash_map?
Oppure, c'è un contenitore Hash C++ migliore là fuori?
Sono hashing int32s, di solito circa 100.000 di loro.
Aggiornamento: ho creato una replica in C# e C++. Esegue due prove, prendono 19ms e 13ms in C# e circa 11.000ms in C++. Ci deve essere qualcosa di veramente sbagliato con il mio codice C++ :)
(Entrambi sono stati eseguiti come build di rilascio, entrambi sono applicazioni Console)
C# in uscita:
Found 511 values in the intersection, in 19 ms
Found 508 values in the intersection, in 13 ms
C++ in uscita:
Found 308 values in the intersection, in 11764.7ms
Found 316 values in the intersection, in 11742.8ms
Output C++ (utilizzando stdext :: hash_map invece di std :: map)
Found 300 values in the intersection, in 383.552ms
Found 306 values in the intersection, in 2277.02ms
C++ uscita (usando stdext :: hash_map, una build di rilascio x64)
Found 292 values in the intersection, in 1037.67ms
Found 302 values in the intersection, in 3663.71ms
Note:
- Set2 non è sempre popolata piuttosto come avrei voluto in C++, mi aspettavo di avere un incrocio al 50% con Set1 (come fa in C#), ma ho dovuto moltiplicare il mio numero a caso del 10 per qualche motivo per ottenere anche loro di parte non si intersecano
C#:
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;
}
C++:
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;
}
Potresti fornire alcuni punti di riferimento? –
Ciò che richiede forse 10ms in C# sembra di prendere 1.000ms in C++. Proverò a fare un confronto più controllato domani, magari post-codice per ogni C# e C++. –
Ho pubblicato alcuni benchmark. –