Advertisement
Guest User

Dupe checker benchmark

a guest
Jun 6th, 2013
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. var rand = new Random();
  2.  
  3. var stufff = Enumerable.Range(1, 1000).Select(i => rand.Next(1, 100000));
  4.  
  5.  
  6. var sw = Stopwatch.StartNew();
  7.  
  8. for (int i = 0; i < 10000; i++)
  9. {  
  10.     var nonNulls = stufff.Where(x => x != null);
  11.     var dupes = nonNulls.Count() != nonNulls.Distinct().Count();
  12. }
  13.  
  14. sw.Stop();
  15.  
  16. sw.ElapsedMilliseconds.Dump("Distinct");
  17.  
  18. var sw2 = Stopwatch.StartNew();
  19.  
  20. for (int i = 0; i < 10000; i++)
  21. {  
  22.  
  23.     var dupes = stufff
  24.         .Where(item => item != null)
  25.         .GroupBy(item => item)
  26.         .Any(g => g.Count() > 1);
  27. }
  28.  
  29. sw2.Stop();
  30.  
  31. sw2.ElapsedMilliseconds.Dump("Count");
  32.  
  33. var sw3 = Stopwatch.StartNew();
  34.  
  35. for (int i = 0; i < 10000; i++)
  36. {  
  37.  
  38.     var dupes = stufff
  39.         .Where(item => item != null)
  40.         .GroupBy(item => item)
  41.         .Any(g => g.Skip(1).Any());
  42. }
  43.  
  44. sw3.Stop();
  45.  
  46. sw3.ElapsedMilliseconds.Dump("Skip");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement