Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created September 24, 2017 18:21
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 angelovstanton/a5af3ca12cce3edaeddc6c0b91b7ee36 to your computer and use it in GitHub Desktop.
Save angelovstanton/a5af3ca12cce3edaeddc6c0b91b7ee36 to your computer and use it in GitHub Desktop.
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