私はいくつかの数字のセットを交差させており、これを行うには、マップに数字が表示されるたびにカウントを保存します。
パフォーマンスが非常に遅いことがわかりました。
詳細:-セットの1つに150,000の数字が含まれています-そのセットと別のセットの交差には、最初は約300ミリ秒、2回目は約5000ミリ秒かかります-まだプロファイリングを行っていませんが、ブレークするたびにmalloc.cで交差を実行している間のデバッガー!
では、どうすればこのパフォーマンスを向上させることができますか?別のデータ構造に切り替えますか?マップのメモリ割り当てパフォーマンスをどのように改善しますか?
アップデート:
- std::mapまたはboost::unordered_mapにスペースを事前に割り当てるように依頼する方法はありますか?
- または、これらを効率的に使用するためのヒントはありますか?
Update2:
C#HashSet<T>やDictionary<K、V>のようなFastC++コンテナを参照してください。
Update3:
set_intersectionのベンチマークを行い、ひどい結果が得られました。
(set_intersection) Found 313 values in the intersection, in 11345ms
(set_intersection) Found 309 values in the intersection, in 12332ms
コード:
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();
}