Created
September 24, 2017 18:21
-
-
Save angelovstanton/a5af3ca12cce3edaeddc6c0b91b7ee36 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
private void Profile(string testResultsFileName, int iterations, Action actionToProfile) | |
{ | |
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); | |
var resultsPath = Path.Combine(desktopPath, "BenchmarkTestResults", | |
string.Concat(testResultsFileName, "_", Guid.NewGuid().ToString(), ".txt")); | |
var writer = new StreamWriter(resultsPath); | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
var watch = new Stopwatch(); | |
watch.Start(); | |
for (var i = 0; i < iterations; i++) | |
{ | |
actionToProfile(); | |
} | |
watch.Stop(); | |
writer.WriteLine("Total: {0:0.00} ms ({1:N0} ticks) (over {2:N0} iterations)", | |
watch.ElapsedMilliseconds, watch.ElapsedTicks, iterations); | |
var avgElapsedMillisecondsPerRun = watch.ElapsedMilliseconds / iterations; | |
var avgElapsedTicksPerRun = watch.ElapsedMilliseconds / iterations; | |
writer.WriteLine("AVG: {0:0.00} ms ({1:N0} ticks) (over {2:N0} iterations)", | |
avgElapsedMillisecondsPerRun, avgElapsedTicksPerRun, iterations); | |
writer.Flush(); | |
writer.Close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment