// iterations = 1
// dictSize = 1000000
// sw1 = 0.84s
// sw2 = 0.69s
// iterations = 100
// dictSize = 100000
// sw1 = 6.06s
// sw2 = 4.05s
// iterations = 10000
// dictSize = 1000
// sw1 = 3.70s
// sw2 = 2.15s
// iterations = 1000000
// dictSize = 10
// sw1 = 4.56s
// sw2 = 2.72s
const int iterations = 100;
const int dictSize = 1000;
var rand = new Random();
var d = new Dictionary<string, int>();
var l = new List<string> {""};
// Create random dictionary and list
for (var i = 0; i < dictSize; i++)
{
var s = Guid.NewGuid().ToString();
d.Add(s, rand.Next());
// Fill the list with some of the keys
if (i % 10 == 0)
{
l.Insert(rand.Next(l.Count - 1), s);
}
// Also fill it with some junk
if (i % 20 == 0)
{
l.Insert(rand.Next(l.Count - 1), Guid.NewGuid().ToString());
}
}
var sw1 = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
var d2 = new Dictionary<string, int>(d);
var mySet = new HashSet<string>(l);
d2 = d2.Where(x => mySet.Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value);
}
sw1.Stop();
var sw2 = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
var d2 = new Dictionary<string, int>(d);
d2.RemoveAll(l);
}
sw2.Stop();
Console.WriteLine(sw1.Elapsed);
Console.WriteLine(sw2.Elapsed);