Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace NiebezpiecznaKolekcja
  10. {
  11. class Program
  12. {
  13. // static readonly object blocker = new object();
  14. static void Main(string[] args)
  15. {
  16. List<Thread> watki = new List<Thread>();
  17. List<int> liczby = new List<int>(1000000);
  18. Random rand = new Random();
  19.  
  20. Stopwatch stopWatch = new Stopwatch();
  21. stopWatch.Start();
  22. for (int i = 0; i < 100; i++)
  23. {
  24. var watek = new Thread(() =>
  25. {
  26. for (int l = 0; l < 10000; l++)
  27. // lock(blocker)
  28. {
  29. liczby.Add(rand.Next());
  30. }
  31.  
  32. });
  33. watki.Add(watek);
  34. watek.Start();
  35. }
  36. foreach (var watek in watki)
  37. {
  38. watek.Join();
  39. }
  40.  
  41. stopWatch.Stop();
  42. TimeSpan ts = stopWatch.Elapsed;
  43.  
  44. string elapsedTime = String.Format("{0:00}.{1:00}",
  45. ts.Seconds,
  46. ts.Milliseconds / 10);
  47. Console.WriteLine("RunTime: " + elapsedTime);
  48.  
  49. Console.WriteLine("Liczba elementów w liście zwykłej:" + liczby.Count);
  50. Console.ReadLine();
  51. }
  52. }
  53. }
  54.  
  55. ***********
  56. bez locka
  57. 00.83
  58. liczba elementów 907406
  59.  
  60. z
  61.  
  62. 01.23
  63. liczba elementów 1000000
  64.  
  65.  
  66. using System;
  67. using System.Collections.Concurrent;
  68. using System.Collections.Generic;
  69. using System.Diagnostics;
  70. using System.Linq;
  71. using System.Text;
  72. using System.Threading;
  73. using System.Threading.Tasks;
  74.  
  75. namespace ConcurrentBag
  76. {
  77. class Program
  78. {
  79. static void Main(string[] args)
  80. {
  81.  
  82. List<Thread> watki = new List<Thread>();
  83. ConcurrentBag<int> liczby = new ConcurrentBag<int>();
  84. Random rand = new Random();
  85.  
  86. Stopwatch stopWatch = new Stopwatch();
  87. stopWatch.Start();
  88. for (int i = 0; i < 100; i++)
  89. {
  90. var watek = new Thread(() =>
  91. {
  92. for (int l = 0; l < 10000; l++)
  93.  
  94. liczby.Add(rand.Next());
  95. });
  96. watki.Add(watek);
  97. watek.Start();
  98. }
  99. foreach (var watek in watki)
  100. {
  101. watek.Join();
  102. }
  103.  
  104. stopWatch.Stop();
  105. TimeSpan ts = stopWatch.Elapsed;
  106.  
  107. string elapsedTime = String.Format("{0:00}.{1:00}",
  108. ts.Seconds,
  109. ts.Milliseconds / 10);
  110. Console.WriteLine("RunTime: " + elapsedTime);
  111.  
  112. Console.WriteLine("Liczba elementów w liście zwykłej:" + liczby.Count);
  113. Console.ReadLine();
  114. }
  115. }
  116. }
  117.  
  118.  
  119.  
  120. *************************
  121.  
  122. 01.95
  123. 1000000
  124.  
  125.  
  126.  
  127. using System;
  128. using System.Collections.Concurrent;
  129. using System.Collections.Generic;
  130. using System.Diagnostics;
  131. using System.Linq;
  132. using System.Text;
  133. using System.Threading;
  134. using System.Threading.Tasks;
  135.  
  136. namespace KolekcjaUnikalnaLock
  137. {
  138.  
  139. public static class Extensions
  140. {
  141.  
  142. public static bool IsUnique(this List<Thread> lista)
  143. {
  144. if (lista.Distinct().Count() == lista.Count()) //jeżeli liczba unikalnych jest równa liczbie wszystkich to wszystkie są unikalne.
  145. {
  146. return true;
  147. }
  148. // Console.WriteLine($"Liczba disctinct: {lista.Distinct().Count()} jest różna od liczby elementów: {lista.Count()}");
  149. return false;
  150. }
  151.  
  152. public static bool IsUnique(this ConcurrentBag<int> cb)
  153. {
  154. if (cb.Distinct().Count() == cb.Count()) return true;
  155. else return false;
  156. }
  157. }
  158.  
  159.  
  160. class Program
  161. {
  162. static readonly object blocker = new Object();
  163.  
  164.  
  165. static void Main(string[] args)
  166. {
  167.  
  168. List<Thread> watki = new List<Thread>();
  169. ConcurrentBag<int> liczby = new ConcurrentBag<int>();
  170. Stopwatch sw = new Stopwatch();
  171. sw.Start();
  172. Random rand = new Random();
  173. for (int i = 0; i < 100; i++)
  174. {
  175.  
  176. var watek = new Thread(() =>
  177. {
  178.  
  179. for (int l = 0; l < 100; l++)
  180. {
  181.  
  182. int liczba = rand.Next(10001);
  183.  
  184. lock (blocker)
  185. {
  186. while (liczby.Any(x => x == liczba))
  187. {
  188.  
  189. liczba = rand.Next(10001);
  190.  
  191.  
  192. };
  193. liczby.Add(liczba);
  194.  
  195.  
  196.  
  197. //Console.WriteLine(liczby.Distinct().Count());
  198.  
  199. }
  200. }
  201. });
  202.  
  203.  
  204. watki.Add(watek);
  205. watek.Start();
  206. }
  207.  
  208. foreach (var watek in watki)
  209. {
  210. watek.Join();
  211. }
  212. sw.Stop();
  213.  
  214. TimeSpan ts = sw.Elapsed;
  215.  
  216. string elapsedTime = String.Format("{0:00}.{1:00}",
  217. ts.Seconds,
  218. ts.Milliseconds / 10);
  219. Console.WriteLine("RunTime: " + elapsedTime);
  220.  
  221. if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
  222. Console.WriteLine("wszystkie liczby są unikalne");
  223. else
  224. Console.WriteLine("Nie wszystkie liczby są unikalne");
  225.  
  226.  
  227. Console.ReadLine();
  228.  
  229.  
  230. }
  231. }
  232. }
  233.  
  234.  
  235.  
  236. ********************************************************
  237.  
  238. 16.35
  239. 17.42
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement