Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 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 LinearMaxInstr() //
  57. {
  58. OpComparisonEQ = 0;
  59. bool Present = IsPresent_LinearInstr(TestVector, TestVector.Length - 1);
  60. Console.Write("\t" + OpComparisonEQ);
  61. }
  62. static void LinearMaxTime()
  63. {
  64. double ElapsedSeconds;
  65. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  66. for (int n = 0; n < (NIter + 1 + 1); ++n)
  67. {
  68. long StartingTime = Stopwatch.GetTimestamp();
  69. bool Present = IsPresent_LinearTime(TestVector, TestVector.Length - 1);
  70. long EndingTime = Stopwatch.GetTimestamp();
  71. IterationElapsedTime = EndingTime - StartingTime;
  72. ElapsedTime += IterationElapsedTime;
  73. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  74. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  75. }
  76. ElapsedTime -= (MinTime + MaxTime);
  77. ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  78. Console.Write("\t" + ElapsedSeconds.ToString("F4"));
  79. }
  80. static void BinaryMaxInstr()
  81. {
  82. OpComparisonEQ = 0;
  83. bool Present = IsPresent_BinaryInstr(TestVector, TestVector.Length - 1);
  84. Console.Write("\t" + OpComparisonEQ);
  85. }
  86. static void BinaryMaxTime()
  87. {
  88. double ElapsedSeconds;
  89. long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
  90. for (int n = 0; n < (NIter + 1 + 1); ++n)
  91. {
  92. long StartingTime = Stopwatch.GetTimestamp();
  93. bool Present = IsPresent_BinaryTime(TestVector, TestVector.Length - 1);
  94. long EndingTime = Stopwatch.GetTimestamp();
  95. IterationElapsedTime = EndingTime - StartingTime;
  96. ElapsedTime += IterationElapsedTime;
  97. if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
  98. if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
  99. }
  100. ElapsedTime -= (MinTime + MaxTime);
  101. ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
  102. Console.Write("\t" + ElapsedSeconds.ToString("F4"));
  103. }
  104. static void Main(string[] args)
  105. {
  106. Console.WriteLine("Size\tMaxI\tMaxT\tAvgI\tBmaxI\tBmaxT\tBavgI");
  107. for (int ArraySize = 26843545; ArraySize <= 268435450; ArraySize += 26843545)
  108. {
  109. Console.Write(ArraySize);
  110. // -- Create Table
  111. TestVector = new int[ArraySize];
  112. // -- Write Table
  113. for (int i = 0; i < TestVector.Length; ++i)
  114. {
  115. TestVector[i] = i;
  116. }
  117. LinearMaxInstr();
  118. LinearMaxTime();
  119. BinaryMaxInstr();
  120. BinaryMaxTime();
  121. Console.Write("\n");
  122.  
  123. }
  124. Console.ReadKey();
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement