Created
January 26, 2017 23:49
-
-
Save ScottKaye/582c7381e9beefaef64da4b88600b619 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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