Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.36 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. namespace ProjektWzorcowy
  4. {
  5. class Program
  6. {
  7. static long OpComparisonEQ;
  8. static long OpComparisonLin;
  9. static long OpComparisonBin;
  10. static bool IsPresent_LinInstr(long[] Vector, int Number)
  11. {
  12. OpComparisonLin = 0;
  13. for (int i = 0; i < Vector.Length; i++)
  14. {
  15. OpComparisonLin++;
  16. if (Vector[i] == Number) return true;
  17. }
  18. return false;
  19. }
  20. static bool IsPresent_LinTim(long[] Vector, int Number)
  21. {
  22. for (int i = 0; i < Vector.Length; i++)
  23. {
  24. if (Vector[i] == Number) return true;
  25. }
  26. return false;
  27. }
  28. static long LoopsNumber;
  29. static bool IsPresent_LinTimAvg(long[] Vector, int Number)
  30. {
  31. LoopsNumber = 0;
  32. for (int skip = 0; skip < Vector.Length; skip += 2500000, LoopsNumber++)
  33. {
  34. for (int i = 0; i < skip; i++)
  35. {
  36. OpComparisonEQ++;
  37. if (Vector[i] == skip)
  38. {
  39. OpComparisonEQ = OpComparisonEQ / LoopsNumber;
  40. return true;
  41. }
  42. }
  43. }
  44. OpComparisonEQ = OpComparisonEQ / LoopsNumber;
  45. return false;
  46. }
  47.  
  48. static long[] TestVector;
  49. static bool IsPresent_BinInstr(long[] Vector, int Number)
  50. {
  51. OpComparisonBin = 0;
  52. int Left = 0, Right = Vector.Length - 1, Middle;
  53. while (Left <= Right)
  54. {
  55. OpComparisonBin++;
  56. Middle = (Left + Right) / 2;
  57. if (Vector[Middle] == Number) return true;
  58. else if (Vector[Middle] > Number) Right = Middle - 1; //OpComparisonBin++;
  59. else
  60. {
  61. Left = Middle + 1;
  62. OpComparisonBin++;
  63. }
  64. }
  65. return false;
  66. }
  67. static double AllTime;
  68. static double AvgTime;
  69. static bool IsPresent_BinTimAvg(long[] Vector, int Number)
  70. {
  71. OpComparisonBin = 0;
  72. int Left = 0, Right = Vector.Length - 1, Middle;
  73. uint x = 1;
  74. while (Left <= Right)
  75. {
  76. long StartingTimeAvg = Stopwatch.GetTimestamp();
  77. OpComparisonBin++;
  78. Middle = (Left + Right) / 2;
  79. if (Vector[Middle] == Number) return true;
  80. else if (Vector[Middle] > Number) Right = Middle - 1; //OpComparisonBin++;
  81. else
  82. {
  83. Left = Middle + 1;
  84. OpComparisonBin++;
  85. }
  86. long EndingTimeAvg = Stopwatch.GetTimestamp();
  87. long DifTime = (EndingTimeAvg - StartingTimeAvg) * x;
  88. AllTime += DifTime;
  89. x = x * 2;
  90. // AvgTime = AllTime / Vector.Length;
  91.  
  92. }
  93. return false;
  94. }
  95. static void Main(string[] args)
  96. {
  97.  
  98. for (int ArraySize = 26843545; ArraySize <= 268435450; ArraySize += 26843545)
  99. {
  100. Console.Write("\t" + ArraySize + "\tMaxLin");
  101. TestVector = new long[ArraySize];
  102. for (int i = 0; i < TestVector.Length; ++i)
  103. TestVector[i] = i;
  104.  
  105. // Console.Write("\t" + IsPresent_LinInstr(TestVector, TestVector.Length - 1 )+"\t");
  106. //Console.Write("\t" + OpComparisonEQ);
  107. //Console.Write("\t"+IsPresent_BinInstr(TestVector, TestVector.Length - 1));
  108. //Console.Write("\t" + OpComparisonLin);
  109. //BinMaxTime();
  110. //LinMaxTime();
  111. //LinInstrMax();
  112. // BinMaxIter();
  113. // LinInstrAvg();
  114. // LinTimeAvg();
  115. BinTimeAvg();
  116.  
  117.  
  118. }
  119.  
  120. }
  121. static bool IsPresent_BinTim(int[] Vector, int Number)
  122. {
  123. for (int i = 0; i < Vector.Length; i++)
  124. {
  125. OpComparisonEQ++;
  126. if (Vector[i] == Number) return true;
  127. }
  128. return false;
  129. }
  130. static void BinMaxTime()
  131. {
  132. int NIter = 10;
  133. double ElapsedSeconds;
  134. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  135. for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
  136. {
  137. long StartingTime = Stopwatch.GetTimestamp();
  138. bool Present = IsPresent_BinInstr(TestVector, TestVector.Length - 1);
  139. long EndingTime = Stopwatch.GetTimestamp();
  140. IterationElapsedTime = EndingTime - StartingTime;
  141. ElapsedTime += IterationElapsedTime;
  142. //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
  143. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  144. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  145. }
  146. ElapsedTime -= (MinTime + MaxTime);
  147. ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  148.  
  149. Console.Write("\t" + ElapsedSeconds.ToString("F4"));
  150. }
  151. static void BinMaxIter()
  152. {
  153. int NIter = 10;
  154. //double ElapsedSeconds;
  155. //long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  156. for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
  157. {
  158. //long StartingTime = Stopwatch.GetTimestamp();
  159. bool Present = IsPresent_BinInstr(TestVector, TestVector.Length - 1);
  160. // long EndingTime = Stopwatch.GetTimestamp();
  161. // IterationElapsedTime = EndingTime - StartingTime;
  162. // ElapsedTime += IterationElapsedTime;
  163. //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
  164. // if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  165. // if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  166. }
  167. // ElapsedTime -= (MinTime + MaxTime);
  168. //ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  169. Console.Write(OpComparisonBin);
  170. }
  171. static void LinMaxTime()
  172. {
  173. int NIter = 10;
  174. double ElapsedSeconds;
  175. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  176. for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
  177. {
  178. long StartingTime = Stopwatch.GetTimestamp();
  179. bool Present = IsPresent_LinTim(TestVector, TestVector.Length - 1);
  180. long EndingTime = Stopwatch.GetTimestamp();
  181. IterationElapsedTime = EndingTime - StartingTime;
  182. ElapsedTime += IterationElapsedTime;
  183. //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
  184. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  185. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  186. }
  187. ElapsedTime -= (MinTime + MaxTime);
  188. ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  189. Console.Write("\t" + ElapsedSeconds.ToString("F4"));
  190. }
  191. static void LinInstrMax()
  192. {
  193. int NIter = 10;
  194. //double ElapsedSeconds;
  195. //long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  196. for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
  197. {
  198. // long StartingTime = Stopwatch.GetTimestamp();
  199. bool Present = IsPresent_LinInstr(TestVector, TestVector.Length - 1);
  200. // long EndingTime = Stopwatch.GetTimestamp();
  201. // IterationElapsedTime = EndingTime - StartingTime;
  202. // ElapsedTime += IterationElapsedTime;
  203. //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
  204. // if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  205. // if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  206. }
  207. // ElapsedTime -= (MinTime + MaxTime);
  208. //ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  209. Console.Write(OpComparisonLin);
  210. }
  211. static void LinInstrAvg()
  212. {
  213. int NIter = 10;
  214. //double ElapsedSeconds;
  215. //long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  216. for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
  217. {
  218. // long StartingTime = Stopwatch.GetTimestamp();
  219. bool Present = IsPresent_LinTimAvg(TestVector, TestVector.Length - 1);
  220. // long EndingTime = Stopwatch.GetTimestamp();
  221. // IterationElapsedTime = EndingTime - StartingTime;
  222. // ElapsedTime += IterationElapsedTime;
  223. //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
  224. // if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  225. // if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  226. }
  227. // ElapsedTime -= (MinTime + MaxTime);
  228. //ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  229. Console.Write(OpComparisonEQ);
  230. }
  231. static void LinTimeAvg()
  232. {
  233. int NIter = 10;
  234. double ElapsedSeconds;
  235. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  236. for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
  237. {
  238. long StartingTime = Stopwatch.GetTimestamp();
  239. bool Present = IsPresent_LinTimAvg(TestVector, TestVector.Length - 1);
  240. long EndingTime = Stopwatch.GetTimestamp();
  241. IterationElapsedTime = EndingTime - StartingTime;
  242. ElapsedTime += IterationElapsedTime;
  243. //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
  244. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  245. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  246. }
  247. ElapsedTime -= (MinTime + MaxTime);
  248. ElapsedSeconds = ElapsedTime * (1.0 / (LoopsNumber*NIter * Stopwatch.Frequency));
  249. Console.Write("\t" + ElapsedSeconds.ToString("F4"));
  250. }
  251. static void BinTimeAvg()
  252. {
  253. int NIter = 10;
  254. double ElapsedSeconds;
  255. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  256. AllTime = 0;
  257. for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
  258. {
  259. long StartingTime = Stopwatch.GetTimestamp();
  260. bool Present = IsPresent_BinTimAvg(TestVector, TestVector.Length - 1);
  261. long EndingTime = Stopwatch.GetTimestamp();
  262. IterationElapsedTime = EndingTime - StartingTime;
  263. ElapsedTime += IterationElapsedTime;
  264. //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
  265. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  266. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  267. }
  268.  
  269. AvgTime = ((AllTime/120) / ((TestVector.Length/10 ) ));
  270. ElapsedTime -= (MinTime + MaxTime);
  271. ElapsedSeconds = AvgTime * (1.0 / (NIter * Stopwatch.Frequency));
  272. Console.Write("\t" + AvgTime.ToString("F4"));
  273. }
  274. static void BinInstAvg()
  275. {
  276. int NIter = 10;
  277. double ElapsedSeconds;
  278. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  279. for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
  280. {
  281. long StartingTime = Stopwatch.GetTimestamp();
  282. bool Present = IsPresent_BinTimAvg(TestVector, TestVector.Length - 1);
  283. long EndingTime = Stopwatch.GetTimestamp();
  284. IterationElapsedTime = EndingTime - StartingTime;
  285. ElapsedTime += IterationElapsedTime;
  286. //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
  287. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  288. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  289. }
  290.  
  291. AvgTime = AllTime / ((TestVector.Length-1)*12);
  292. ElapsedTime -= (MinTime + MaxTime);
  293. ElapsedSeconds = AvgTime * (1.0 / (NIter * Stopwatch.Frequency));
  294. Console.Write("\t" + AvgTime.ToString("F4"));
  295. }
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement