/// <summary> /// Returns GCD of the two or more numbers with the gcdMethod algorithm (Stein's or Euclidean algorithm). /// </summary> /// <param name="gcdMethod"> GCD algorithm. </param> /// <param name="numbers"> Array of the numbers. </param> /// <returns> GCD of the numbers. </returns> public static int MultiGCD(GCDMethod gcdMethod, params int[] numbers) { if (numbers == null) { throw new ArgumentNullException("Array shouldn't be null"); } if (numbers.Length < 2) { throw new ArgumentException("Array should contain more than 1 number."); } var watch = Stopwatch.StartNew(); int result = numbers[0]; for (int i = 1; i < numbers.Length; i++) { result = gcdMethod(numbers[i], result); } watch.Stop(); ElapsedTime = watch.ElapsedMilliseconds; return(result); }
public static GCDMethodDecorated Decorate(GCDMethod onDecorating ) { return (Func<int, int, int> gcdAlgorithm, int[] numbers,out TimeSpan time)=> { Stopwatch timer=new Stopwatch(); time=new TimeSpan(0); timer.Start(); int result=onDecorating(gcdAlgorithm, numbers); timer.Stop(); time+=timer.Elapsed; return result; }; }
/// <summary> /// Метод для определения времени работы выбранного алгоритма нахождения НОД(<paramref name="a"/>, <paramref name="b"/>) /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="method"></param> /// <returns></returns> public static TimeSpan GetTime(uint a, uint b, GCDMethod method) { switch (method) { case GCDMethod.BinaryGCD: _stopwatch.Start(); _ = BinaryGCD(a, b); _stopwatch.Stop(); break; case GCDMethod.GCD: _stopwatch.Start(); _ = GCD(a, b); _stopwatch.Stop(); break; } TimeSpan elapsed = _stopwatch.Elapsed; _stopwatch.Reset(); return(elapsed); }
public static int Calculate(GCDMethod delegateMethod, out long ticks, int arg1, int arg2) { Stopwatch watch = new Stopwatch(); watch.Start(); arg1 = Math.Abs(arg1); arg2 = Math.Abs(arg2); int result = delegateMethod(arg1, arg2); watch.Stop(); ticks = new TimeSpan(watch.ElapsedTicks).Ticks; return result; }
public static int Calculate(GCDMethod delegateMethod, out long time, params int[] args) { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < args.Length - 1; i++) { args[i] = delegateMethod(Math.Abs(args[i]), Math.Abs(args[i + 1])); args[i + 1] = args[i]; } watch.Stop(); time = new TimeSpan(watch.ElapsedTicks).Ticks; return args[args.Length - 1]; }
/// <summary> /// Вычисление времени работы выбранного алгоритма /// </summary> /// <param name="batchSize">Количество итераций</param> /// <param name="argumentCount">Количество аргументов</param> /// <param name="method">Алгоритм</param> /// <param name="randomMin">Минимальное значение чисел, использующихся в алгоритме</param> /// <param name="randomMax">Максимальное значение чисел, использующихся в алгоритме</param> /// <returns>Время, затраченное на вычисления</returns> public static TimeSpan GetRandomBatchTime(int batchSize = _defaultBatchSize, int argumentCount = _defaultArgumentCount, GCDMethod method = GCDMethod.GCD, int randomMin = _randomMin, int randomMax = _randomMax) { if (batchSize < 1 || argumentCount < 2 || randomMin < 0) { throw new ArgumentOutOfRangeException(); } // (4 * batchSize * argumentCount) bytes if cached uint[][] values = new uint[batchSize][]; for (int i = 0; i < batchSize; ++i) { values[i] = (new uint[argumentCount]).Select(val => (uint)_rnd.Next(randomMin, randomMax)).ToArray(); } HelperDelegate helper = null; switch (method) { case GCDMethod.BinaryGCD: helper = BinaryGCD; break; case GCDMethod.GCD: helper = GCD; break; } _stopwatch.Start(); for (int i = 0; i < batchSize; ++i) { _ = helper(values[i]); } _stopwatch.Stop(); TimeSpan elapsed = _stopwatch.Elapsed; _stopwatch.Reset(); return(elapsed); }