Guest User

Untitled

a guest
Jan 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using SqlServerTypes;
  8. using Microsoft.SqlServer.Types;
  9. using System.Data.SqlTypes;
  10. using System.Threading;
  11.  
  12. namespace test_parallell
  13. {
  14. class Program
  15. {
  16. static void Main()
  17. {
  18. SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
  19.  
  20. List<SqlGeometry> pGeo = new List<SqlGeometry>();
  21.  
  22. Stopwatch regularLoop = new Stopwatch();
  23. Stopwatch thread2NoLoop = new Stopwatch();
  24. Stopwatch thread2Loop = new Stopwatch();
  25. Stopwatch thread3NoLoop = new Stopwatch();
  26. Stopwatch thread3Loop = new Stopwatch();
  27. Stopwatch thread4NoLoop = new Stopwatch();
  28. Stopwatch thread4Loop = new Stopwatch();
  29. Stopwatch thread5NoLoop = new Stopwatch();
  30. Stopwatch thread5Loop = new Stopwatch();
  31. Stopwatch thread6NoLoop = new Stopwatch();
  32. Stopwatch thread6Loop = new Stopwatch();
  33. Stopwatch thread7NoLoop = new Stopwatch();
  34. Stopwatch thread7Loop = new Stopwatch();
  35. Stopwatch thread8NoLoop = new Stopwatch();
  36. Stopwatch thread8Loop = new Stopwatch();
  37.  
  38. SqlGeometry regularAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  39. SqlGeometry thread2NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  40. SqlGeometry thread2Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  41. SqlGeometry thread3NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  42. SqlGeometry thread3Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  43. SqlGeometry thread4NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  44. SqlGeometry thread4Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  45. SqlGeometry thread5NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  46. SqlGeometry thread5Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  47. SqlGeometry thread6NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  48. SqlGeometry thread6Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  49. SqlGeometry thread7NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  50. SqlGeometry thread7Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  51. SqlGeometry thread8NoAdd = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  52. SqlGeometry thread8Add = SqlGeometry.STPolyFromText(new SqlChars(new SqlString("POLYGON ((5 5, 10 5, 10 10, 5 5))")), 4326);
  53.  
  54. Random rnd = new Random();
  55. SqlGeometry k = null;
  56.  
  57. for (int n = 1; n < 1001; n++)
  58. {
  59. int checker = rnd.Next(-1000, 1000);
  60. int checkerd = checker * 2;
  61. string sqlstring = "POLYGON ((" + checker + " " + checker + ", " + checkerd + " " + checker + ", " + checkerd + " " + checkerd + ", " + checker + " " + checker + "))";
  62. k = SqlGeometry.STPolyFromText(new SqlChars(new SqlString(sqlstring)), 4326).MakeValid();
  63. pGeo.Add(k);
  64. }
  65.  
  66. //REGULAR FOREACH
  67. regularLoop.Start();
  68. int totalSeen1 = 0;
  69. foreach (var x in pGeo)
  70. { regularAdd = regularAdd.STUnion(x); totalSeen1++; }
  71. regularLoop.Stop();
  72. //REGULAR FOREACH
  73.  
  74. //2
  75. thread2Loop.Start();
  76. int totalSeen2 = 0;
  77. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 2 }, x =>
  78. { lock (thread2Add) { thread2Add = thread2Add.STUnion(x); totalSeen2++; } });
  79. thread2Loop.Stop();
  80.  
  81. thread2NoLoop.Start();
  82. int totalSeen2No = 0;
  83. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 2 }, x =>
  84. { thread2NoAdd = thread2NoAdd.STUnion(x); totalSeen2No++; });
  85. thread2NoLoop.Stop();
  86.  
  87. //3
  88. thread3Loop.Start();
  89. int totalSeen3 = 0;
  90. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 3 }, x =>
  91. { lock (thread3Add) { thread3Add = thread3Add.STUnion(x); totalSeen3++; } });
  92. thread3Loop.Stop();
  93.  
  94. thread3NoLoop.Start();
  95. int totalSeen3No = 0;
  96. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 3 }, x =>
  97. { thread3NoAdd = thread3NoAdd.STUnion(x); totalSeen3No++; });
  98. thread3NoLoop.Stop();
  99.  
  100. //4
  101. thread4Loop.Start();
  102. int totalSeen4 = 0;
  103. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 4 }, x =>
  104. { lock (thread4Add) { thread4Add = thread4Add.STUnion(x); totalSeen4++; } });
  105. thread4Loop.Stop();
  106.  
  107. thread4NoLoop.Start();
  108. int totalSeen4No = 0;
  109. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 4 }, x =>
  110. { thread4NoAdd = thread4NoAdd.STUnion(x); totalSeen4No++; });
  111. thread4NoLoop.Stop();
  112.  
  113. //5
  114. thread5Loop.Start();
  115. int totalSeen5 = 0;
  116. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 5 }, x =>
  117. { lock (thread5Add) { thread5Add = thread5Add.STUnion(x); totalSeen5++; } });
  118. thread5Loop.Stop();
  119.  
  120. thread5NoLoop.Start();
  121. int totalSeen5No = 0;
  122. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 5 }, x =>
  123. { thread5NoAdd = thread5NoAdd.STUnion(x); totalSeen5No++; });
  124. thread5NoLoop.Stop();
  125.  
  126. //6
  127. thread6Loop.Start();
  128. int totalSeen6 = 0;
  129. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 6 }, x =>
  130. { lock (thread6Add) { thread6Add = thread6Add.STUnion(x); totalSeen6++; } });
  131. thread6Loop.Stop();
  132.  
  133. thread6NoLoop.Start();
  134. int totalSeen6No = 0;
  135. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 6 }, x =>
  136. { thread6NoAdd = thread6NoAdd.STUnion(x); totalSeen6No++; });
  137. thread6NoLoop.Stop();
  138.  
  139. //7
  140. thread7Loop.Start();
  141. int totalSeen7 = 0;
  142. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 7 }, x =>
  143. { lock (thread7Add) { thread7Add = thread7Add.STUnion(x); totalSeen7++; } });
  144. thread7Loop.Stop();
  145.  
  146. thread7NoLoop.Start();
  147. int totalSeen7No = 0;
  148. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 7 }, x =>
  149. { thread7NoAdd = thread7NoAdd.STUnion(x); totalSeen7No++; });
  150. thread7NoLoop.Stop();
  151.  
  152. //8
  153. thread8Loop.Start();
  154. int totalSeen8 = 0;
  155. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 8 }, x =>
  156. { lock (thread8Add) { thread8Add = thread8Add.STUnion(x); totalSeen8++; } });
  157. thread8Loop.Stop();
  158.  
  159. thread8NoLoop.Start();
  160. int totalSeen8No = 0;
  161. Parallel.ForEach(pGeo, new ParallelOptions { MaxDegreeOfParallelism = 8 }, x =>
  162. { thread8NoAdd = thread8NoAdd.STUnion(x); totalSeen8No++; });
  163. thread8NoLoop.Stop();
  164.  
  165. double cover = double.Parse(regularAdd.STArea().ToString());
  166.  
  167. double cover2 = double.Parse(thread2Add.STArea().ToString());
  168. double cover2No = double.Parse(thread2NoAdd.STArea().ToString());
  169. double cover3 = double.Parse(thread3Add.STArea().ToString());
  170. double cover3No = double.Parse(thread3NoAdd.STArea().ToString());
  171. double cover4 = double.Parse(thread4Add.STArea().ToString());
  172. double cover4No = double.Parse(thread4NoAdd.STArea().ToString());
  173. double cover5 = double.Parse(thread5Add.STArea().ToString());
  174. double cover5No = double.Parse(thread5NoAdd.STArea().ToString());
  175. double cover6 = double.Parse(thread6Add.STArea().ToString());
  176. double cover6No = double.Parse(thread6NoAdd.STArea().ToString());
  177. double cover7 = double.Parse(thread7Add.STArea().ToString());
  178. double cover7No = double.Parse(thread7NoAdd.STArea().ToString());
  179. double cover8 = double.Parse(thread8Add.STArea().ToString());
  180. double cover8No = double.Parse(thread8NoAdd.STArea().ToString());
  181.  
  182. double perc2 = Math.Round(((cover2 / cover) * 100), 2);
  183. double perc2No = Math.Round(((cover2No / cover) * 100), 2);
  184. double perc3 = Math.Round(((cover3 / cover) * 100), 2);
  185. double perc3No = Math.Round(((cover3No / cover) * 100), 2);
  186. double perc4 = Math.Round(((cover4 / cover) * 100), 2);
  187. double perc4No = Math.Round(((cover4No / cover) * 100), 2);
  188. double perc5 = Math.Round(((cover5 / cover) * 100), 2);
  189. double perc5No = Math.Round(((cover5No / cover) * 100), 2);
  190. double perc6 = Math.Round(((cover6 / cover) * 100), 2);
  191. double perc6No = Math.Round(((cover6No / cover) * 100), 2);
  192. double perc7 = Math.Round(((cover7 / cover) * 100), 2);
  193. double perc7No = Math.Round(((cover7No / cover) * 100), 2);
  194. double perc8 = Math.Round(((cover8 / cover) * 100), 2);
  195. double perc8No = Math.Round(((cover8No / cover) * 100), 2);
  196.  
  197. Console.WriteLine("TestName;Polygon Points;Checked Points;Duration;Coverage");
  198. Console.WriteLine("Regular Loop;" + regularAdd.STNumPoints() + ";" + totalSeen1 + ";" + regularLoop.ElapsedMilliseconds + ";100%");
  199.  
  200. Console.WriteLine("2Loop No Lock;" + thread2NoAdd.STNumPoints() + ";" + totalSeen2No + ";" + thread2NoLoop.ElapsedMilliseconds + ";" + perc2No + "%");
  201. Console.WriteLine("2Loop With Lock;" + thread2Add.STNumPoints() + ";" + totalSeen2 + ";" + thread2Loop.ElapsedMilliseconds + ";" + perc2 + "%");
  202.  
  203. Console.WriteLine("3Loop No Lock;" + thread3NoAdd.STNumPoints() + ";" + totalSeen3No + ";" + thread3NoLoop.ElapsedMilliseconds + ";" + perc3No + "%");
  204. Console.WriteLine("3Loop With Lock;" + thread3Add.STNumPoints() + ";" + totalSeen3 + ";" + thread3Loop.ElapsedMilliseconds + ";" + perc3 + "%");
  205.  
  206. Console.WriteLine("4Loop No Lock;" + thread4NoAdd.STNumPoints() + ";" + totalSeen4No + ";" + thread4NoLoop.ElapsedMilliseconds + ";" + perc4No + "%");
  207. Console.WriteLine("4Loop With Lock;" + thread4Add.STNumPoints() + ";" + totalSeen4 + ";" + thread4Loop.ElapsedMilliseconds + ";" + perc4 + "%");
  208.  
  209. Console.WriteLine("5Loop No Lock;" + thread5NoAdd.STNumPoints() + ";" + totalSeen5No + ";" + thread5NoLoop.ElapsedMilliseconds + ";" + perc5No + "%");
  210. Console.WriteLine("5Loop With Lock;" + thread5Add.STNumPoints() + ";" + totalSeen5 + ";" + thread5Loop.ElapsedMilliseconds + ";" + perc5 + "%");
  211.  
  212. Console.WriteLine("6Loop No Lock;" + thread6NoAdd.STNumPoints() + ";" + totalSeen6No + ";" + thread6NoLoop.ElapsedMilliseconds + ";" + perc6No + "%");
  213. Console.WriteLine("6Loop With Lock;" + thread6Add.STNumPoints() + ";" + totalSeen6 + ";" + thread6Loop.ElapsedMilliseconds + ";" + perc6 + "%");
  214.  
  215. Console.WriteLine("7Loop No Lock;" + thread7NoAdd.STNumPoints() + ";" + totalSeen7No + ";" + thread7NoLoop.ElapsedMilliseconds + ";" + perc7No + "%");
  216. Console.WriteLine("7Loop With Lock;" + thread7Add.STNumPoints() + ";" + totalSeen7 + ";" + thread7Loop.ElapsedMilliseconds + ";" + perc7 + "%");
  217.  
  218. Console.WriteLine("8Loop No Lock;" + thread8NoAdd.STNumPoints() + ";" + totalSeen8No + ";" + thread8NoLoop.ElapsedMilliseconds + ";" + perc8No + "%");
  219. Console.WriteLine("8Loop With Lock;" + thread8Add.STNumPoints() + ";" + totalSeen8 + ";" + thread8Loop.ElapsedMilliseconds + ";" + perc8 + "%");
  220.  
  221. Console.WriteLine("\n");
  222. Console.WriteLine("Press any key to quit...");
  223. Console.ReadKey();
  224. }
  225. }
  226. }
Add Comment
Please, Sign In to add comment