1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| class Program
{
static void Main()
{
int length = 100000;
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < length; i++)
{
ExceptionFree.StaticMeth(true);
}
sw.Stop();
Console.WriteLine(string.Format("Static no ex handler: {0} ticks ({1} ms)", sw.ElapsedTicks, sw.ElapsedMilliseconds));
sw.Reset();
sw.Start();
for (int i = 0; i < length; i++)
{
ExceptionThrow.StaticMeth(false);
}
sw.Stop();
Console.WriteLine(string.Format("Static with handler no ex throw : {0} ticks ({1} ms)", sw.ElapsedTicks, sw.ElapsedMilliseconds));
sw.Reset();
sw.Start();
for (int i = 0; i < length; i++)
{
ExceptionThrow.StaticMeth(true);
}
sw.Stop();
Console.WriteLine(string.Format("Static with handler ex throw: {0} ticks ({1} ms)", sw.ElapsedTicks, sw.ElapsedMilliseconds));
sw.Reset();
ExceptionFree e = new ExceptionFree();
sw.Start();
for (int i = 0; i < length; i++)
{
e.InstanceMeth(true);
}
sw.Stop();
Console.WriteLine(string.Format("Instance no ex handler: {0} ticks ({1} ms)", sw.ElapsedTicks, sw.ElapsedMilliseconds));
sw.Reset();
ExceptionThrow et = new ExceptionThrow();
sw.Start();
for (int i = 0; i < length; i++)
{
et.InstanceMeth(false);
}
sw.Stop();
Console.WriteLine(string.Format("Instance with handler no ex throw : {0} ticks ({1} ms)", sw.ElapsedTicks, sw.ElapsedMilliseconds));
sw.Reset();
sw.Start();
for (int i = 0; i < length; i++)
{
et.InstanceMeth(true);
}
sw.Stop();
Console.WriteLine(string.Format("Instance with handler ex throw: {0} ticks ({1} ms)", sw.ElapsedTicks, sw.ElapsedMilliseconds));
sw.Reset();
Console.Read();
}
}
class ExceptionFree
{
public static string StaticMeth(bool dummy)
{
if (dummy) { }
return "Passed";
}
public string InstanceMeth(bool dummy)
{
if (dummy) { }
return "Passed";
}
}
class ExceptionThrow
{
public static string StaticMeth(bool throwEx)
{
try
{
if (throwEx)
throw new Exception("Test");
}
catch (Exception ex)
{
}
return "Passed";
}
public string InstanceMeth(bool throwEx)
{
try
{
if (throwEx)
throw new Exception("Test");
}
catch (Exception ex)
{
}
return "Passed";
}
} |
Partager