Skip to content

Instantly share code, notes, and snippets.

@CallumWatkins
Last active February 18, 2017 16:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save CallumWatkins/15ea75162d151bcd812c4fa6544b7f11 to your computer and use it in GitHub Desktop.
A method for testing the speed of an action. License: MIT
/// <summary>
/// Tests the speed of an action.
/// </summary>
/// <param name="action">The method to be tested.</param>
/// <param name="iterations">The number of times to perform the action in each test.</param>
/// <param name="repetitions">The number of times to repeat the test.</param>
/// <param name="name">The name of the test.</param>
/// <param name="warmupMilliseconds">The number of milliseconds to warm up for.</param>
static void TestSpeed(Action action, long iterations, int repetitions, string name, int warmupMilliseconds = 5000)
{
if (action == null) { throw new ArgumentNullException(nameof(action)); }
if (iterations < 1) { throw new ArgumentOutOfRangeException(nameof(iterations)); }
if (repetitions < 1) { throw new ArgumentOutOfRangeException(nameof(repetitions)); }
if (name == null) { throw new ArgumentNullException(nameof(name)); }
if (warmupMilliseconds < 0) { throw new ArgumentOutOfRangeException(nameof(warmupMilliseconds)); }
// Store current processor and thread properties
IntPtr processorAffinity = System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity;
ProcessPriorityClass priorityClass = System.Diagnostics.Process.GetCurrentProcess().PriorityClass;
ThreadPriority priority = Thread.CurrentThread.Priority;
// Set processor and thread properties
System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
System.Diagnostics.Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Console.WriteLine($"***** Testing speed of '{name}' at {iterations:N0} iterations {repetitions} {(repetitions > 1 ? "times" : "time")} *****");
double totalSeconds = 0;
var sw = new Stopwatch();
Console.WriteLine($"| Warming up for {warmupMilliseconds} milliseconds...");
// Warmup
sw.Start();
while (sw.ElapsedMilliseconds < warmupMilliseconds)
{
action();
}
Console.WriteLine("| Starting test...");
for (int i = 0; i < repetitions; i++)
{
sw.Reset();
sw.Start();
for (long j = 0; j < iterations; j++)
{
action();
}
sw.Stop();
Console.WriteLine($"| {i + 1}: {sw.Elapsed.TotalSeconds} seconds.");
totalSeconds += sw.Elapsed.TotalSeconds;
}
Console.WriteLine("| Tests complete.");
if (repetitions > 1) { Console.WriteLine($"| Average: {totalSeconds/repetitions} seconds."); }
Console.WriteLine($"***** Finished testing '{name}' *****");
// Reset processor and thread properties
System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = processorAffinity;
System.Diagnostics.Process.GetCurrentProcess().PriorityClass = priorityClass;
Thread.CurrentThread.Priority = priority;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment