Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bench Marking finding dups
void Main()
{
List<PlugIn> plugIns = new List<PlugIn> ();
//Create some plugins
var keys = Enumerable.Range(0,100).Select(e => Guid.NewGuid().ToString()).ToArray();
for (int i = 0; i < 1000000; i++)
{
plugIns.Add(new PlugIn(keys[new Random().Next(keys.Length-1)],"Some thing",Console.WriteLine));
}
Stopwatch watch = new Stopwatch ();
watch.Start();
var duplicates = plugIns.GroupBy(i => i.ID)
.Where(g => g.Count() > 1)
.ToDictionary(g => g.Key, g => g.Select(a => a.Description).ToList());
watch.Stop();
watch.ElapsedMilliseconds.Dump("Previous time");
Stopwatch w = new Stopwatch ();
w.Start();
var hasDup = plugIns.ToLookup(plugIn => plugIn.ID).Count != plugIns.Count;
w.Stop();
w.ElapsedMilliseconds.Dump("Time after refactoring");
}
class PlugIn
{
public string ID { get; set; }
public string Description { get; set; }
public Action DoThis { get; set;}
public PlugIn(string id, string des, Action act)
{
ID = id;
Description = des;
DoThis = act;
}
}
// Define other methods and classes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment