Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Created June 3, 2013 18:54
Show Gist options
  • Save JohanLarsson/5700390 to your computer and use it in GitHub Desktop.
Save JohanLarsson/5700390 to your computer and use it in GitHub Desktop.
public static class StopwatchExt
{
public static string GetTimeString(this Stopwatch stopwatch, int numberofDigits = 1)
{
double s = stopwatch.ElapsedTicks / (double)Stopwatch.Frequency;
if (s > 1)
return Math.Round(s, numberofDigits) + " s";
if (s > 1e-3)
return Math.Round(1e3 * s, numberofDigits) + " ms";
if (s > 1e-6)
return Math.Round(1e6 * s, numberofDigits) + " µs";
if (s > 1e-9)
return Math.Round(1e9 * s, numberofDigits) + " ns";
return stopwatch.ElapsedTicks + " ticks";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment