/// <summary> /// Get the time measurement of a certain action executed an amount of times. /// </summary> /// <param name = "label">The label to indicate which measurement it is upon calling ToString().</param> /// <param name = "action">The action to measure the time from.</param> /// <param name = "times">The amount of times the action should run.</param> /// <returns>The time measurement of the executed actions.</returns> public static StatisticsStopwatch.Measurement From(string label, Action action, int times) { if (action == null) { throw new ArgumentNullException(nameof(action)); } if (times < 0) { throw new ArgumentOutOfRangeException(nameof(times), "Should be greater than zero."); } StatisticsStopwatch stopwatch = StatisticsStopwatch.Start(label); // Perform the action the required amount of times. for (int i = 0; i < times; ++i) { action(); if (i != times - 1) // Start a new measurement every iteration, for all but last. { stopwatch.StartNextMeasurement(); } } return(stopwatch.Stop()); }
/// <summary> /// Get the time measurement of a certain action executed an amount of times. /// </summary> /// <param name = "label">The label to indicate which measurement it is upon calling ToString().</param> /// <param name = "action">The action to measure the time from.</param> /// <param name = "times">The amount of times the action should run.</param> /// <returns>The time measurement of the executed actions.</returns> public static StatisticsStopwatch.Measurement From(string label, Action action, int times) { Contract.Requires(action != null); Contract.Requires(times >= 0); StatisticsStopwatch stopwatch = null; Loop loop = Loop.NumberOfTimes(times, action); // Start a new measurement every iteration. loop.Before.First(() => stopwatch = StatisticsStopwatch.Start(label)); loop.After.AllButLast(() => stopwatch.StartNextMeasurement()); loop.Run(); return(stopwatch.Stop()); }