Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created March 24, 2016 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudipto80/26adb21bb8e6a4f16c38 to your computer and use it in GitHub Desktop.
Save sudipto80/26adb21bb8e6a4f16c38 to your computer and use it in GitHub Desktop.
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