私はC#でHashSetとDictionaryをよく使用しましたが、非常に高速であることがわかりました...
std::mapとstd::hash_mapを使用してみましたが、比較が非常に遅いことがわかりました。これは期待される動作のように聞こえますか?std :: hash_mapの使用で間違っている可能性のあることがありますか?
または、より良いC ++ハッシュコンテナがありますか?
私はint32をハッシュしています。通常はそのうちの約100,000です。
更新:C#とC++で再現を作成しました。2つのトライアルを実行します。C#では19ミリ秒と13ミリ秒、C++では約11,000ミリ秒かかります。私のC++コードに本当に何か問題があるに違いありません:)
(どちらもリリースビルドとして実行されました。どちらもコンソールアプリです)
C#出力:
Found 511 values in the intersection, in 19 ms
Found 508 values in the intersection, in 13 ms
C ++出力:
Found 308 values in the intersection, in 11764.7ms
Found 316 values in the intersection, in 11742.8ms
C ++出力(std::mapの代わりにstdext::hash_mapを使用)
Found 300 values in the intersection, in 383.552ms
Found 306 values in the intersection, in 2277.02ms
C ++出力(stdext :: hash_map、リリースx64ビルドを使用)
Found 292 values in the intersection, in 1037.67ms
Found 302 values in the intersection, in 3663.71ms
ノート:
- Set2は、C ++で必要なように入力されていません。これは、Set1と50%の交差があると予想していましたが(C#の場合と同様)、何らかの理由で乱数を10倍する必要がありました。部分的に交差しない
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;
}