private static void RunPerformanceQuestion(string number, int iterationCount = 2 * 1000 * 1000)
{
Console.WriteLine("Running " + number);
Console.WriteLine();
var types = typeof(Program).Assembly.GetTypes().Where(x => x.Namespace != null && x.Namespace.Contains("Performance") && x.Namespace.EndsWith(number) && !x.IsNested);
var durationOfFirstAnswer = (TimeSpan?)null;
foreach (var type in types)
{
var runDelegate = (Action)type.GetMethod("Run").CreateDelegate(typeof(Action));
var stopwatch = Stopwatch.StartNew();
for (var i = 0; i < iterationCount; i++)
{
runDelegate();
}
stopwatch.Stop();
if (durationOfFirstAnswer == null)
{
durationOfFirstAnswer = stopwatch.Elapsed;
Console.WriteLine($"{number}/{type.Name}: {stopwatch.Elapsed}");
}
else
{
var previousTicks = durationOfFirstAnswer.Value.Ticks;
var newTicks = stopwatch.Elapsed.Ticks;
var increase = (newTicks - previousTicks) / (double)previousTicks;
Console.WriteLine($"{number}/{type.Name}: {stopwatch.Elapsed} ({increase:P0})");
}
}
}