using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Main
{
sealed class Program
{
static void SomeAction()
{
for (int i = 0; i < 1000; i++)
}
static void Handle()
{
// просто заглушка
}
static void WithException(int iterations)
{
var watch = Stopwatch.StartNew();
try
{
for (int i = 0; i < iterations; i++)
SomeAction();
throw new Exception();
}
catch (Exception e)
{
Handle();
}
watch.Stop();
NumberFormatInfo nfi = (NumberFormatInfo)
CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
Console.WriteLine("with exception {1} iteration {0} ms", watch.ElapsedMilliseconds, iterations.ToString("n0", nfi));
Console.WriteLine();
}
static void WithoutException(int iterations)
{
var watch = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
SomeAction();
Handle();
watch.Stop();
NumberFormatInfo nfi = (NumberFormatInfo)
CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
Console.WriteLine("without exception {1} iteration {0} ms", watch.ElapsedMilliseconds, iterations.ToString("n0", nfi));
}
static void Main(string[] args)
{
WithoutException(1); // этот и следующий вызов для "прогрева"
WithException(1);
WithoutException(1);
WithException(1);
WithoutException(1000);
WithException(1000);
WithoutException(100000);
WithException(100000);
WithoutException(500000);
WithException(500000);
Console.ReadLine();
}
}
}