Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- namespace ProjektWzorcowy
- {
- class Program
- {
- static long OpComparisonEQ;
- static long OpComparisonLin;
- static long OpComparisonBin;
- static bool IsPresent_LinInstr(long[] Vector, int Number)
- {
- OpComparisonLin = 0;
- for (int i = 0; i < Vector.Length; i++)
- {
- OpComparisonLin++;
- if (Vector[i] == Number) return true;
- }
- return false;
- }
- static bool IsPresent_LinTim(long[] Vector, int Number)
- {
- for (int i = 0; i < Vector.Length; i++)
- {
- if (Vector[i] == Number) return true;
- }
- return false;
- }
- static long LoopsNumber;
- static bool IsPresent_LinTimAvg(long[] Vector, int Number)
- {
- LoopsNumber = 0;
- for (int skip = 0; skip < Vector.Length; skip += 2500000, LoopsNumber++)
- {
- for (int i = 0; i < skip; i++)
- {
- OpComparisonEQ++;
- if (Vector[i] == skip)
- {
- OpComparisonEQ = OpComparisonEQ / LoopsNumber;
- return true;
- }
- }
- }
- OpComparisonEQ = OpComparisonEQ / LoopsNumber;
- return false;
- }
- static long[] TestVector;
- static bool IsPresent_BinInstr(long[] Vector, int Number)
- {
- OpComparisonBin = 0;
- int Left = 0, Right = Vector.Length - 1, Middle;
- while (Left <= Right)
- {
- OpComparisonBin++;
- Middle = (Left + Right) / 2;
- if (Vector[Middle] == Number) return true;
- else if (Vector[Middle] > Number) Right = Middle - 1; //OpComparisonBin++;
- else
- {
- Left = Middle + 1;
- OpComparisonBin++;
- }
- }
- return false;
- }
- static double AllTime;
- static double AvgTime;
- static bool IsPresent_BinTimAvg(long[] Vector, int Number)
- {
- OpComparisonBin = 0;
- int Left = 0, Right = Vector.Length - 1, Middle;
- uint x = 1;
- while (Left <= Right)
- {
- long StartingTimeAvg = Stopwatch.GetTimestamp();
- OpComparisonBin++;
- Middle = (Left + Right) / 2;
- if (Vector[Middle] == Number) return true;
- else if (Vector[Middle] > Number) Right = Middle - 1; //OpComparisonBin++;
- else
- {
- Left = Middle + 1;
- OpComparisonBin++;
- }
- long EndingTimeAvg = Stopwatch.GetTimestamp();
- long DifTime = (EndingTimeAvg - StartingTimeAvg) * x;
- AllTime += DifTime;
- x = x * 2;
- // AvgTime = AllTime / Vector.Length;
- }
- return false;
- }
- static void Main(string[] args)
- {
- for (int ArraySize = 26843545; ArraySize <= 268435450; ArraySize += 26843545)
- {
- Console.Write("\t" + ArraySize + "\tMaxLin");
- TestVector = new long[ArraySize];
- for (int i = 0; i < TestVector.Length; ++i)
- TestVector[i] = i;
- // Console.Write("\t" + IsPresent_LinInstr(TestVector, TestVector.Length - 1 )+"\t");
- //Console.Write("\t" + OpComparisonEQ);
- //Console.Write("\t"+IsPresent_BinInstr(TestVector, TestVector.Length - 1));
- //Console.Write("\t" + OpComparisonLin);
- //BinMaxTime();
- //LinMaxTime();
- //LinInstrMax();
- // BinMaxIter();
- // LinInstrAvg();
- // LinTimeAvg();
- BinTimeAvg();
- }
- }
- static bool IsPresent_BinTim(int[] Vector, int Number)
- {
- for (int i = 0; i < Vector.Length; i++)
- {
- OpComparisonEQ++;
- if (Vector[i] == Number) return true;
- }
- return false;
- }
- static void BinMaxTime()
- {
- int NIter = 10;
- double ElapsedSeconds;
- long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
- {
- long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_BinInstr(TestVector, TestVector.Length - 1);
- long EndingTime = Stopwatch.GetTimestamp();
- IterationElapsedTime = EndingTime - StartingTime;
- ElapsedTime += IterationElapsedTime;
- //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
- 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 BinMaxIter()
- {
- int NIter = 10;
- //double ElapsedSeconds;
- //long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
- {
- //long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_BinInstr(TestVector, TestVector.Length - 1);
- // long EndingTime = Stopwatch.GetTimestamp();
- // IterationElapsedTime = EndingTime - StartingTime;
- // ElapsedTime += IterationElapsedTime;
- //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
- // if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
- // if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
- }
- // ElapsedTime -= (MinTime + MaxTime);
- //ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
- Console.Write(OpComparisonBin);
- }
- static void LinMaxTime()
- {
- int NIter = 10;
- double ElapsedSeconds;
- long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
- {
- long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_LinTim(TestVector, TestVector.Length - 1);
- long EndingTime = Stopwatch.GetTimestamp();
- IterationElapsedTime = EndingTime - StartingTime;
- ElapsedTime += IterationElapsedTime;
- //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
- 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 LinInstrMax()
- {
- int NIter = 10;
- //double ElapsedSeconds;
- //long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
- {
- // long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_LinInstr(TestVector, TestVector.Length - 1);
- // long EndingTime = Stopwatch.GetTimestamp();
- // IterationElapsedTime = EndingTime - StartingTime;
- // ElapsedTime += IterationElapsedTime;
- //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
- // if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
- // if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
- }
- // ElapsedTime -= (MinTime + MaxTime);
- //ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
- Console.Write(OpComparisonLin);
- }
- static void LinInstrAvg()
- {
- int NIter = 10;
- //double ElapsedSeconds;
- //long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
- {
- // long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_LinTimAvg(TestVector, TestVector.Length - 1);
- // long EndingTime = Stopwatch.GetTimestamp();
- // IterationElapsedTime = EndingTime - StartingTime;
- // ElapsedTime += IterationElapsedTime;
- //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
- // if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
- // if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
- }
- // ElapsedTime -= (MinTime + MaxTime);
- //ElapsedSeconds = ElapsedTime * (1.0 / (NIter * Stopwatch.Frequency));
- Console.Write(OpComparisonEQ);
- }
- static void LinTimeAvg()
- {
- int NIter = 10;
- double ElapsedSeconds;
- long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
- {
- long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_LinTimAvg(TestVector, TestVector.Length - 1);
- long EndingTime = Stopwatch.GetTimestamp();
- IterationElapsedTime = EndingTime - StartingTime;
- ElapsedTime += IterationElapsedTime;
- //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
- if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
- if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
- }
- ElapsedTime -= (MinTime + MaxTime);
- ElapsedSeconds = ElapsedTime * (1.0 / (LoopsNumber*NIter * Stopwatch.Frequency));
- Console.Write("\t" + ElapsedSeconds.ToString("F4"));
- }
- static void BinTimeAvg()
- {
- int NIter = 10;
- double ElapsedSeconds;
- long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- AllTime = 0;
- for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
- {
- long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_BinTimAvg(TestVector, TestVector.Length - 1);
- long EndingTime = Stopwatch.GetTimestamp();
- IterationElapsedTime = EndingTime - StartingTime;
- ElapsedTime += IterationElapsedTime;
- //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
- if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
- if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
- }
- AvgTime = ((AllTime/120) / ((TestVector.Length/10 ) ));
- ElapsedTime -= (MinTime + MaxTime);
- ElapsedSeconds = AvgTime * (1.0 / (NIter * Stopwatch.Frequency));
- Console.Write("\t" + AvgTime.ToString("F4"));
- }
- static void BinInstAvg()
- {
- int NIter = 10;
- double ElapsedSeconds;
- long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
- for (int n = 0; n < (NIter + 1 + 1); ++n) // odejmujemy wartości skrajne
- {
- long StartingTime = Stopwatch.GetTimestamp();
- bool Present = IsPresent_BinTimAvg(TestVector, TestVector.Length - 1);
- long EndingTime = Stopwatch.GetTimestamp();
- IterationElapsedTime = EndingTime - StartingTime;
- ElapsedTime += IterationElapsedTime;
- //Console.Write("Iter[" + n + "]:" + IterationElapsedTime + "\t");
- if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
- if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
- }
- AvgTime = AllTime / ((TestVector.Length-1)*12);
- ElapsedTime -= (MinTime + MaxTime);
- ElapsedSeconds = AvgTime * (1.0 / (NIter * Stopwatch.Frequency));
- Console.Write("\t" + AvgTime.ToString("F4"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement