fork download
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4.  
  5. class Program
  6. {
  7. static void Main()
  8. {
  9.  
  10. decimal totalElapsedTicks = 0;
  11.  
  12. // Create new stopwatch
  13. Stopwatch stopwatch = new Stopwatch();
  14.  
  15.  
  16.  
  17. // Do something
  18. for (int i = 0; i < 100000; i++)
  19. {
  20. try
  21. {
  22. // Begin timing
  23. stopwatch.Start();
  24. RethrowException();
  25.  
  26.  
  27. }catch
  28. {
  29. // Stop timing
  30. stopwatch.Stop();
  31.  
  32. }
  33. }
  34. // Write result
  35. Console.WriteLine("try/catch technique: {0:#,###,###,###}", stopwatch.ElapsedTicks);
  36.  
  37.  
  38. stopwatch.Reset();
  39. // Do something
  40. for (int i = 0; i < 100000; i++)
  41. {
  42. try
  43. {
  44. // Begin timing
  45. stopwatch.Start();
  46. PassThroughException();
  47.  
  48.  
  49. }catch
  50. {
  51. // Stop timing
  52. stopwatch.Stop();
  53.  
  54. }
  55. }
  56. // Write result
  57. Console.WriteLine("try/finaly technique: {0:#,###,###,###}", stopwatch.ElapsedTicks);
  58.  
  59.  
  60.  
  61. }
  62.  
  63. static void RethrowException()
  64. {
  65. try
  66. {
  67. throw new Exception();
  68. }
  69. catch
  70. {
  71. DummyMethod();
  72. throw;
  73. }
  74. }
  75.  
  76. static void PassThroughException()
  77. {
  78. bool exceptionThrown = false;
  79. try
  80. {
  81. exceptionThrown = true;
  82. throw new Exception();
  83. }
  84. finally
  85. {
  86. if(exceptionThrown)
  87. {
  88. DummyMethod();
  89. }
  90.  
  91. }
  92. }
  93.  
  94. static int DummyMethod()
  95. {
  96. return 0;
  97. }
  98.  
  99. }
  100.  
Success #stdin #stdout 0.83s 34208KB
stdin
Standard input is empty
stdout
try/catch technique: 3,926,966
try/finaly technique: 3,394,355