HashSet
さまざまなインデックス例外を含むものがたくさんあります。HashSet
入力データに応じて、これらのハッシュセットを 1 つの大きなものに結合します。テスト目的で、アナログにも移植HashSet
しました。List
HashSet
andの唯一の目的はList
、乱数生成からインデックスを除外することです。
それが私がリストの場合に行うことです:
list2 = new List<int>();
for (int d = 0; d < list1.Count; d++)
{
if (dicCat4[30].ContainsKey(list1[d]))
{
list2.AddRange(dicCat4[30][list1[d]]);
}
}
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
while (list2.Contains(rand))
{
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
}
// action with random
ご覧のとおり、すべての例外 (インデックス) は を使用して 1 つのリストにマージされますAddRange()
。メソッドを使用Contains()
して、乱数がリスト内にあるかどうかを確認します。
同じ操作が HashSet で実行できます。
excludehash = new HashSet<int>();
for (int d = 0; d < list1.Count; d++)
{
if (dicCat4[30].ContainsKey(list1[d]))
{
excludehash.UnionWith(dicCat3[30][list1[d]]);
}
}
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
while (excludehash.Contains(rand))
{
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
}
// action with random
この場合、代わりにメソッドをAddRange()
使用してインデックスの例外UnionWith()
をマージします。HashSet
奇妙なことに、何千回も繰り返した後、メソッドの全体的なパフォーマンスList
が向上しました! 、しかし多くの情報源によると、HashSet
より高速に実行する必要があります。パフォーマンス プロファイラーは、最大のパフォーマンス ホグが HashSet のUnionWith()
メソッドであることを示しました。
私はただ興味があります -ソリューションをより速く実行する方法はありますか? HashSet
(簡単なアイデアが思い浮かびました:代わりにContains(rand)
、すべての個別のハッシュセットで使用できるため、UnionWith()
メソッドをスキップします)
PS ハッシュセットとリストは以下から取得されます。
static Dictionary<int, Dictionary<int, HashSet<int>>> dicCat3;
static Dictionary<int, Dictionary<int, List<int>>> dicCat4;
編集:ハードコア反復ソリューション
int inter = list1.Count;
int cco = 0;
while (inter != cco)
{
cco = 0;
rand = 2 * RandString.Next(0 / 2, (dicCat[30].Count) / 2);
for (int d = 0; d < list1.Count; d++)
{
if (dicCat4[30].ContainsKey(list1[d]))
{
if (!dicCat3[30]][list1[d]].Contains(rand))
{
cco++;
}
else
{
break;
}
}
else
{
cco++;
}
}
}