Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace ConsoleApp6
  5. {
  6. class Program
  7. {
  8. static ulong OpComparisonEQ = 0, OpAssignment = 0, OpComparisonLT = 0, OpIncrement =0;
  9.  
  10. //-------------------------------Lin------------------------------
  11. public static double CheckTimeLin(uint[] Vector, uint Number)
  12. {
  13.  
  14. long StartingTime = Stopwatch.GetTimestamp();
  15.  
  16. _IsPresentLinTime(Vector, Number);
  17.  
  18.  
  19. long EndingTime = Stopwatch.GetTimestamp();
  20. long ElapsedTime = EndingTime - StartingTime;
  21. double ElapsedSeconds = ElapsedTime * (1.0 / Stopwatch.Frequency);
  22.  
  23. return ElapsedSeconds;
  24. }
  25.  
  26. public static bool _IsPresentLinTime(uint[] Vector, uint Number)
  27. {
  28. for (int i = 0; i < Vector.Length; i++)
  29. if (Vector[i] == Number) return true;
  30. return false;
  31. }
  32.  
  33. public static bool IsPresentLinComp(uint[] Vector, uint Number)
  34. {
  35. OpAssignment = OpComparisonLT = 1;
  36. for (int i = 0; i < Vector.Length; i++, OpIncrement++)
  37. {
  38. OpComparisonEQ++;
  39. if (Vector[i] == Number) return true;
  40. OpComparisonLT++;
  41. }
  42. return false;
  43. }
  44. //-------------------------------Bin------------------------------
  45.  
  46. public static double CheckTimeBin(uint[] Vector, uint Number)
  47. {
  48.  
  49. long StartingTime = Stopwatch.GetTimestamp();
  50.  
  51. _IsPresentBinTime(Vector, Number);
  52.  
  53. long EndingTime = Stopwatch.GetTimestamp();
  54. long ElapsedTime = EndingTime - StartingTime;
  55. double ElapsedSeconds = ElapsedTime * (1.0 / Stopwatch.Frequency);
  56.  
  57. return ElapsedSeconds;
  58. }
  59.  
  60. public static bool _IsPresentBinTime(uint[] Vector, uint Number)
  61. {
  62. int Left = 0, Right = Vector.Length - 1, Middle;
  63. while (Left <= Right)
  64. {
  65. Middle = (Left + Right) / 2;
  66. if (Vector[Middle] == Number) return true;
  67. else if (Vector[Middle] > Number) Right = Middle - 1;
  68. else Left = Middle + 1;
  69. }
  70. return false;
  71. }
  72.  
  73. public static bool IsPresentBinComp(uint[] Vector, uint Number)
  74. {
  75. int Left = 0, Right = Vector.Length - 1, Middle;
  76. while (Left <= Right)
  77. {
  78. Middle = (Left + Right) / 2;
  79. OpIncrement++;
  80. if (Vector[Middle] == Number)
  81. return true;
  82. else if (Vector[Middle] > Number)
  83. Right = Middle - 1;
  84. else
  85. Left = Middle + 1;
  86. OpIncrement++;
  87. }
  88. return false;
  89. }
  90.  
  91. //-------------------------------Main------------------------------
  92.  
  93. static void Main(string[] args)
  94. {
  95. double totaltime = 0;
  96. double avgTime = 0;
  97. double avgCompare = 0;
  98. const int loop = 10, startsize = 26843545;
  99.  
  100. Console.WriteLine("Linear:");
  101. Console.WriteLine("Size\tAvgCompare\tAvg.Time[s]\tCompare\tCompare Time[s]\tFound?");
  102.  
  103. for (uint size = startsize; size <= (loop * startsize); size += startsize)
  104. {
  105. uint[] Tab = new uint[size];
  106.  
  107. //uzupełnienie tablicy
  108. for (uint i = 0; i < Tab.Length; ++i)
  109. {
  110. Tab[i] = i;
  111. }
  112.  
  113. //średni czas wyszukiwania
  114. double tempcomp = 0;
  115. for (uint i = 1; i < Tab.Length; i += 1000000)//zbieranie co 1000000 elementu w celu przyśpieszenia pomniarów
  116. {
  117. totaltime += CheckTimeLin(Tab, i);
  118. IsPresentLinComp(Tab, i);
  119. tempcomp++;
  120. }
  121.  
  122. avgCompare = OpIncrement / tempcomp;
  123. avgTime = totaltime / (size / 100000);
  124. OpIncrement = 0;
  125.  
  126. //pesymistyczne wyniki
  127. bool znaleziono = IsPresentLinComp(Tab, size);
  128. double PessymistTime = CheckTimeLin(Tab, size - 1);
  129.  
  130. Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", size, avgCompare, avgTime.ToString("F4"), OpIncrement, PessymistTime.ToString("F4"), znaleziono);
  131.  
  132. }
  133.  
  134. OpIncrement = 0;
  135.  
  136. Console.WriteLine("Binary :");
  137. Console.WriteLine("Size\tAvgCompare\tAvg.Time[s]\tCompare\tCompare Time[s]\tFound?");
  138. for (uint size = startsize; size <= (loop * startsize); size += startsize)
  139. {
  140. uint[] Tab = new uint[size];
  141.  
  142. //średni czas wyszukiwania
  143. for (uint i = 0; i < Tab.Length; ++i)
  144. {
  145. Tab[i] = i;
  146. }
  147.  
  148. //liczenie średniego czasu
  149. for (uint i = 0; i < Tab.Length; ++i)
  150. {
  151. totaltime += CheckTimeBin(Tab, i);
  152. IsPresentBinComp(Tab, i);
  153. }
  154.  
  155. avgTime = totaltime / size;
  156. avgCompare = OpIncrement / size;
  157. OpIncrement = 0;
  158. //pesymistyczne wyniki
  159. bool znaleziono = IsPresentBinComp(Tab, size);
  160. double PessymistTime = CheckTimeBin(Tab, size);
  161.  
  162. Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", size, avgCompare, avgTime.ToString("F4"), OpIncrement, PessymistTime.ToString("F4"),znaleziono);
  163.  
  164.  
  165.  
  166. }
  167. }
  168.  
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement