Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 25th, 2012  |  syntax: C#  |  size: 1.35 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // iterations = 1
  2. // dictSize = 1000000
  3. // sw1 = 0.84s
  4. // sw2 = 0.69s
  5.  
  6. // iterations = 100
  7. // dictSize = 100000
  8. // sw1 = 6.06s
  9. // sw2 = 4.05s
  10.  
  11. // iterations = 10000
  12. // dictSize = 1000
  13. // sw1 = 3.70s
  14. // sw2 = 2.15s
  15.  
  16. // iterations = 1000000
  17. // dictSize = 10
  18. // sw1 = 4.56s
  19. // sw2 = 2.72s
  20.  
  21. const int iterations = 100;
  22. const int dictSize = 1000;
  23.  
  24. var rand = new Random();
  25.  
  26. var d = new Dictionary<string, int>();
  27. var l = new List<string> {""};
  28.  
  29. // Create random dictionary and list
  30. for (var i = 0; i < dictSize; i++)
  31. {
  32.     var s = Guid.NewGuid().ToString();
  33.  
  34.     d.Add(s, rand.Next());
  35.  
  36.     // Fill the list with some of the keys
  37.     if (i % 10 == 0)
  38.     {
  39.         l.Insert(rand.Next(l.Count - 1), s);
  40.     }
  41.  
  42.     // Also fill it with some junk
  43.     if (i % 20 == 0)
  44.     {
  45.         l.Insert(rand.Next(l.Count - 1), Guid.NewGuid().ToString());
  46.     }
  47. }
  48.  
  49. var sw1 = Stopwatch.StartNew();
  50. for (var i = 0; i < iterations; i++)
  51. {
  52.     var d2 = new Dictionary<string, int>(d);
  53.     var mySet = new HashSet<string>(l);
  54.     d2 = d2.Where(x => mySet.Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value);
  55. }
  56. sw1.Stop();
  57.  
  58. var sw2 = Stopwatch.StartNew();
  59. for (var i = 0; i < iterations; i++)
  60. {
  61.     var d2 = new Dictionary<string, int>(d);
  62.     d2.RemoveAll(l);
  63. }
  64. sw2.Stop();
  65.  
  66. Console.WriteLine(sw1.Elapsed);
  67. Console.WriteLine(sw2.Elapsed);