Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- namespace ConsoleApplication1
- {
- class Program
- {
- const int NIter = 10;
- static int[] TestVector;
- static ulong OpComparisonEQ;
- static bool IsPresent_LinearInstr(int[] Vector, int Number) // Liczba kroków w liniowyn
- {
- for (int i = 0; i < Vector.Length; i++)
- {
- OpComparisonEQ++;
- if (Vector[i] == Number) return true;
- }
- return false;
- }
- static bool IsPresent_LinearTime(int[] Vector, int Number) // czas w liniowym
- {
- for (int i = 0; i < Vector.Length; i++)
- if (Vector[i] == Number)
- return true;
- return false;
- }
- static bool IsPresent_BinaryInstr(int[] Vector, int Number) // liczba kroków w binarnym
- {
- int Left = 0, Right = Vector.Length - 1, Middle;
- while (Left <= Right)
- {
- OpComparisonEQ++;
- Middle = (Left + Right) / 2;
- if (Vector[Middle] == Number) return true;
- else if (Vector[Middle] > Number) Right = Middle - 1;
- else Left = Middle + 1;
- }
- return false;
- }
- static bool IsPresent_BinaryTime(int[] Vector, int Number) //czas w binarnym
- {
- int Left = 0, Right = Vector.Length - 1, Middle;
- while (Left <= Right)
- {
- Middle = (Left + Right) / 2;
- if (Vector[Middle] == Number) return true;
- else if (Vector[Middle] > Number) Right = Middle - 1;
- else Left = Middle + 1;
- }
- return false;
- }
- static void LinearMaxInstr() //
- {
- OpComparisonEQ = 0;
- bool Present = IsPresent_LinearInstr(TestVector, TestVector.Length - 1);
- Console.Write("\t" + OpComparisonEQ);
- }
- static void LinearMaxTime()
- {
- double ElapsedSeconds;
- long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- for (int n = 0; n < (NIter + 1 + 1); ++n)
- {
- long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_LinearTime(TestVector, TestVector.Length - 1);
- long EndingTime = Stopwatch.GetTimestamp();
- IterationElapsedTime = EndingTime - StartingTime;
- ElapsedTime += IterationElapsedTime;
- if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
- if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
- }
- ElapsedTime -= (MinTime + MaxTime);
- ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
- Console.Write("\t" + ElapsedSeconds.ToString("F4"));
- }
- static void BinaryMaxInstr()
- {
- OpComparisonEQ = 0;
- bool Present = IsPresent_BinaryInstr(TestVector, TestVector.Length - 1);
- Console.Write("\t" + OpComparisonEQ);
- }
- static void BinaryMaxTime()
- {
- double ElapsedSeconds;
- long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- for (int n = 0; n < (NIter + 1 + 1); ++n)
- {
- long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_BinaryTime(TestVector, TestVector.Length - 1);
- long EndingTime = Stopwatch.GetTimestamp();
- IterationElapsedTime = EndingTime - StartingTime;
- ElapsedTime += IterationElapsedTime;
- if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
- if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
- }
- ElapsedTime -= (MinTime + MaxTime);
- ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
- Console.Write("\t" + ElapsedSeconds.ToString("F4"));
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Size\tMaxI\tMaxT\tAvgI\tBmaxI\tBmaxT\tBavgI");
- for (int ArraySize = 26843545; ArraySize <= 268435450; ArraySize += 26843545)
- {
- Console.Write(ArraySize);
- // -- Create Table
- TestVector = new int[ArraySize];
- // -- Write Table
- for (int i = 0; i < TestVector.Length; ++i)
- {
- TestVector[i] = i;
- }
- LinearMaxInstr();
- LinearMaxTime();
- BinaryMaxInstr();
- BinaryMaxTime();
- Console.Write("\n");
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement