Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5.  
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. const int size = 100;
  11.  
  12. var before = new Before();
  13. var after = new After();
  14.  
  15. AddItems(before, size);
  16. AddItems(after, size);
  17.  
  18. var sw = new Stopwatch();
  19. const int ITERS = 1000000;
  20. while (true)
  21. {
  22. Console.Write("A:");
  23. sw.Restart();
  24. for (int i = 0; i < ITERS; i++)
  25. {
  26. before.Contains(int.MaxValue);
  27. before.Contains(int.MaxValue);
  28. before.Contains(int.MaxValue);
  29. before.Contains(int.MaxValue);
  30. before.Contains(int.MaxValue);
  31.  
  32. before.Contains(10);
  33. before.Contains(10);
  34. before.Contains(10);
  35. before.Contains(10);
  36. before.Contains(10);
  37. }
  38. var elapsedA = sw.Elapsed;
  39. Console.WriteLine(elapsedA);
  40.  
  41. GC.Collect();
  42. GC.WaitForPendingFinalizers();
  43. GC.Collect();
  44.  
  45. Console.Write("B:");
  46. sw.Restart();
  47. for (int i = 0; i < ITERS; i++)
  48. {
  49. after.Contains(int.MaxValue);
  50. after.Contains(int.MaxValue);
  51. after.Contains(int.MaxValue);
  52. after.Contains(int.MaxValue);
  53. after.Contains(int.MaxValue);
  54.  
  55. after.Contains(10);
  56. after.Contains(10);
  57. after.Contains(10);
  58. after.Contains(10);
  59. after.Contains(10);
  60. }
  61. var elapsedB = sw.Elapsed;
  62. Console.WriteLine(elapsedB);
  63.  
  64. GC.Collect();
  65. GC.WaitForPendingFinalizers();
  66. GC.Collect();
  67.  
  68. Console.WriteLine("A/B : {0}", elapsedA.TotalMilliseconds / elapsedB.TotalMilliseconds);
  69. Console.WriteLine("B/A : {0}", elapsedB.TotalMilliseconds / elapsedA.TotalMilliseconds);
  70. Console.WriteLine("(A-B)/A : {0}", Math.Abs((elapsedA.TotalMilliseconds - elapsedB.TotalMilliseconds) / elapsedA.TotalMilliseconds));
  71. Console.WriteLine("(B-A)/B : {0}", Math.Abs((elapsedB.TotalMilliseconds - elapsedA.TotalMilliseconds) / elapsedB.TotalMilliseconds));
  72. Console.WriteLine();
  73.  
  74. Console.ReadLine();
  75. }
  76. }
  77.  
  78. static void AddItems(ICollection<int> collection, int size)
  79. {
  80. for (int i = 1; i <= size; i++)
  81. {
  82. collection.Add(i);
  83. }
  84. }
  85. }
  86.  
  87. class Before : Before<int, int>
  88. {
  89. protected override int GetKeyForItem(int item)
  90. {
  91. return item;
  92. }
  93. }
  94.  
  95. class After : After<int, int>
  96. {
  97. protected override int GetKeyForItem(int item)
  98. {
  99. return item;
  100. }
  101. }
  102.  
  103. abstract class Before<TKey, TItem> : Collection<TItem>
  104. {
  105. private readonly IEqualityComparer<TKey> _comparer = EqualityComparer<TKey>.Default;
  106.  
  107. public Before() { }
  108.  
  109. public bool Contains(TKey key)
  110. {
  111. if (key == null)
  112. throw new ArgumentNullException("key");
  113.  
  114. foreach (TItem item in Items)
  115. {
  116. if (_comparer.Equals(GetKeyForItem(item), key))
  117. return true;
  118. }
  119. return false;
  120. }
  121.  
  122. protected abstract TKey GetKeyForItem(TItem item);
  123. }
  124.  
  125. abstract class After<TKey, TItem> : Collection<TItem>
  126. {
  127. private readonly IEqualityComparer<TKey> _comparer = EqualityComparer<TKey>.Default;
  128.  
  129. public After() : base(new List<TItem>()) { }
  130.  
  131. new private List<TItem> Items
  132. {
  133. get { return (List<TItem>)base.Items; }
  134. }
  135.  
  136. public bool Contains(TKey key)
  137. {
  138. if (key == null)
  139. throw new ArgumentNullException("key");
  140.  
  141. foreach (TItem item in Items)
  142. {
  143. if (_comparer.Equals(GetKeyForItem(item), key))
  144. return true;
  145. }
  146. return false;
  147. }
  148.  
  149. protected abstract TKey GetKeyForItem(TItem item);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement