Skip to content

Instantly share code, notes, and snippets.

@ScottKaye
Created January 26, 2017 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottKaye/582c7381e9beefaef64da4b88600b619 to your computer and use it in GitHub Desktop.
Save ScottKaye/582c7381e9beefaef64da4b88600b619 to your computer and use it in GitHub Desktop.
public static class Benchmark
{
/// <summary>
/// Returns the total number of milliseconds elapsed running a function [iterations] times.
/// </summary>
/// <see cref="http://stackoverflow.com/a/1048708/382456" />
public static double Run(int iterations, Action func)
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
func();
var watch = new Stopwatch();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
watch.Start();
for (int i = 0; i < iterations; ++i) func();
watch.Stop();
return watch.Elapsed.TotalMilliseconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment