Skip to content

Instantly share code, notes, and snippets.

@badamczewski
Created August 25, 2020 10:34
Show Gist options
  • Save badamczewski/f7e3114411f1fa0e7c1d190405d70ecb to your computer and use it in GitHub Desktop.
Save badamczewski/f7e3114411f1fa0e7c1d190405d70ecb to your computer and use it in GitHub Desktop.
public static long Measure(Action a)
{
//
// Quick Wormup.
//
Console.WriteLine(" [1] Warm Up ... ");
for (int i = 0; i < 10; i++)
{
a();
}
double avg = 0;
Console.WriteLine(" [2] Running ... ");
for (int i = 0; i < 10; i++)
{
Stopwatch w = new Stopwatch();
w.Start();
{
a();
}
w.Stop();
avg += w.ElapsedMilliseconds;
Console.WriteLine($" [3] Took: {w.ElapsedMilliseconds} ms");
}
Console.WriteLine($" [4] AVG: {avg / 10} ms");
return 0;
}
@cpatel22
Copy link

Only below code works? if I do not use warmup. I am making some web request and capture the response time of each request.

When I use with a warmup, it not execute all my request.

Stopwatch w = new Stopwatch();
w.Start();
{
a();
}
w.Stop();

        Console.WriteLine($" [3] Took: {w.ElapsedMilliseconds} ms");

@badamczewski
Copy link
Author

@cpatel22

Don't use this to measure web requests, this function wasn't meant to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment