Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 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.  
  8. namespace ConsoleApplication1
  9. {
  10. class Program
  11. {
  12. const int NIter = 10;
  13. static int[] TestVector;
  14. static ulong OpComparisonEQ;
  15. static bool IsPresent_LinearInstr(int[] Vector, int Number) // Liczba kroków w liniowyn
  16. {
  17. for (int i = 0; i < Vector.Length; i++)
  18. {
  19. OpComparisonEQ++;
  20. if (Vector[i] == Number) return true;
  21. }
  22. return false;
  23. }
  24. static bool IsPresent_LinearTime(int[] Vector, int Number) // czas w liniowym
  25. {
  26. for (int i = 0; i < Vector.Length; i++)
  27. if (Vector[i] == Number)
  28. return true;
  29. return false;
  30. }
  31. static bool IsPresent_BinaryInstr(int[] Vector, int Number) // liczba kroków w binarnym
  32. {
  33. int Left = 0, Right = Vector.Length - 1, Middle;
  34. while (Left <= Right)
  35. {
  36. OpComparisonEQ++;
  37. Middle = (Left + Right) / 2;
  38. if (Vector[Middle] == Number) return true;
  39. else if (Vector[Middle] > Number) Right = Middle - 1;
  40. else Left = Middle + 1;
  41. }
  42. return false;
  43. }
  44. static bool IsPresent_BinaryTime(int[] Vector, int Number) //czas w binarnym
  45. {
  46. int Left = 0, Right = Vector.Length - 1, Middle;
  47. while (Left <= Right)
  48. {
  49. Middle = (Left + Right) / 2;
  50. if (Vector[Middle] == Number) return true;
  51. else if (Vector[Middle] > Number) Right = Middle - 1;
  52. else Left = Middle + 1;
  53. }
  54. return false;
  55. }
  56. static void BinaryAvgInstr() // to co bylo na tablicy
  57. {
  58. OpComparisonEQ = 0;
  59. bool Present;
  60. for (int i = 0; i < TestVector.Length; ++i)
  61. Present = IsPresent_BinaryInstr(TestVector, i);
  62. //Console.Write("\t" + Math.Log(TestVector.Length).ToString("F2"));
  63. Console.Write("\t" + ((double)OpComparisonEQ / (double)TestVector.Length).ToString("F1"));
  64. }
  65. static void LinearMaxInstr() //
  66. {
  67. OpComparisonEQ = 0;
  68. bool Present = IsPresent_LinearInstr(TestVector, TestVector.Length - 1);
  69. Console.Write("\t" + OpComparisonEQ);
  70. }
  71. static void LinearMaxTime()
  72. {
  73. double ElapsedSeconds;
  74. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  75. for (int n = 0; n < (NIter + 1 + 1); ++n)
  76. {
  77. long StartingTime = Stopwatch.GetTimestamp();
  78. bool Present = IsPresent_LinearTime(TestVector, TestVector.Length - 1);
  79. long EndingTime = Stopwatch.GetTimestamp();
  80. IterationElapsedTime = EndingTime - StartingTime;
  81. ElapsedTime += IterationElapsedTime;
  82. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  83. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  84. }
  85. ElapsedTime -= (MinTime + MaxTime);
  86. ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  87. Console.Write("\t" + ElapsedSeconds.ToString("F4"));
  88. }
  89.  
  90. static void LinearAvgInstr()
  91. {
  92. OpComparisonEQ = 0;
  93. bool Present = IsPresent_LinearInstr(TestVector, TestVector.Length - 1);
  94. Console.Write("\t" + ((0.5) + ((double)OpComparisonEQ / 2)));
  95. }
  96. static void BinaryMaxInstr()
  97. {
  98. OpComparisonEQ = 0;
  99. bool Present = IsPresent_BinaryInstr(TestVector, TestVector.Length - 1);
  100. Console.Write("\t" + OpComparisonEQ);
  101. }
  102. static void BinaryAvgTim()
  103. {
  104. double ElapsedSeconds;
  105. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  106. for (int n = 0; n < (NIter + 1 + 1); ++n)
  107. {
  108. long StartingTime = Stopwatch.GetTimestamp();
  109. for (int i = 0; i < TestVector.Length; ++i)
  110. IsPresent_BinaryTime(TestVector, i);
  111. long EndingTime = Stopwatch.GetTimestamp();
  112. IterationElapsedTime = EndingTime - StartingTime;
  113. ElapsedTime += IterationElapsedTime;
  114. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  115. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  116. }
  117. ElapsedTime -= (MinTime + MaxTime);
  118. ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  119. Console.Write("\t" + (ElapsedSeconds / TestVector.Length).ToString("F12"));
  120. }
  121. static void BinaryMaxTime()
  122. {
  123. double ElapsedSeconds;
  124. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  125. for (int n = 0; n < (NIter + 1 + 1); ++n)
  126. {
  127. long StartingTime = Stopwatch.GetTimestamp();
  128. bool Present = IsPresent_BinaryTime(TestVector, TestVector.Length - 1);
  129. long EndingTime = Stopwatch.GetTimestamp();
  130. IterationElapsedTime = EndingTime - StartingTime;
  131. ElapsedTime += IterationElapsedTime;
  132. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  133. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  134. }
  135. ElapsedTime -= (MinTime + MaxTime);
  136. ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  137. Console.Write("\t" + ElapsedSeconds.ToString("F4"));
  138. }
  139. static void LinearAvgTim()
  140. {
  141. double ElapsedSeconds;
  142. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  143. for (int n = 0; n < (NIter + 1 + 1); ++n)
  144. {
  145. long StartingTime = Stopwatch.GetTimestamp();
  146. for (int i = 0; i < TestVector.Length; ++i)
  147. IsPresent_LinearTime(TestVector, i);
  148. long EndingTime = Stopwatch.GetTimestamp();
  149. IterationElapsedTime = EndingTime - StartingTime;
  150. ElapsedTime += IterationElapsedTime;
  151. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  152. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  153. }
  154. ElapsedTime -= (MinTime + MaxTime);
  155. ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  156. Console.Write("\t" + (ElapsedSeconds / TestVector.Length).ToString("F12"));
  157. }
  158.  
  159. static void Main(string[] args)
  160. {
  161. //Console.WriteLine("Size\tMaxI\tMaxT\tAvgI\tBmaxI\tBmaxT\tBavgI\tBavgT");
  162. Console.WriteLine("Size\tAvgI\tMaxI");
  163. for (int ArraySize = 26843545; ArraySize <= 268435450; ArraySize += 26843545)
  164. //for (int ArraySize = 1; ArraySize <= 39; ArraySize += 1) // w sredniej binarce przy 7 elementach powinno wyjsc = 2,43
  165. {
  166. Console.Write(ArraySize);
  167. // -- Create Table
  168. TestVector = new int[ArraySize];
  169. // -- Write Table
  170. for (int i = 0; i < TestVector.Length; ++i)
  171. {
  172. TestVector[i] = i;
  173. }
  174. //LinearAvgTim();
  175. //LinearAvgInstr();
  176. //LinearMaxInstr();
  177. //LinearMaxTime();
  178. //LinearAvgInstr();
  179. //BinaryMaxInstr();
  180. // BinaryMaxTime();
  181. BinaryAvgInstr();
  182. //BinaryAvgTime();
  183. Console.Write("\n");
  184.  
  185. }
  186. Console.ReadKey();
  187. }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement