Created
March 24, 2016 08:34
-
-
Save sudipto80/26adb21bb8e6a4f16c38 to your computer and use it in GitHub Desktop.
Bench Marking finding dups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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