Skip to content

Instantly share code, notes, and snippets.

Created July 21, 2016 11:38
Show Gist options
  • Save anonymous/9d60f5d09868ed3a00ec00f413f6afb0 to your computer and use it in GitHub Desktop.
Save anonymous/9d60f5d09868ed3a00ec00f413f6afb0 to your computer and use it in GitHub Desktop.
MeasureExecutionTime
static void MeasureExecutionTime(Action func, string Text)
{
Stopwatch sw = new Stopwatch();
int count = 32, total = 0;
double time = 0.0;
// Warp up
func();
while (true)
{
sw.Restart();
for (int i = 0; i < count; i++)
func();
sw.Stop();
time += sw.Elapsed.TotalSeconds;
total += count;
if (time < 1.0)
{
count = count * 3 / 2;
}
else
{
break;
}
}
time /= total;
Console.Write("{0}: ", Text);
if (time > 1.0)
Console.WriteLine("{0:0.00} s/op, {1:0.000} op/s", time, 1.0 / time);
else if (time > 1e-3)
Console.WriteLine("{0:0.00} ms/op, {1:0.0} op/s", time * 1e3, 1.0 / time);
else if (time > 1e-6)
Console.WriteLine("{0:0.00} us/op, {1:0.0} kop/s", time * 1e6, 1e-3 / time);
else
Console.WriteLine("{0:0.00} ns/op, {1:0.0} mop/s", time * 1e9, 1e-6 / time);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment