fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Main
  10. {
  11. sealed class Program
  12. {
  13. static void SomeAction()
  14. {
  15. var rand = new Random();
  16. for (int i = 0; i < 1000; i++)
  17. rand.Next();
  18. }
  19.  
  20. static void Handle()
  21. {
  22. // просто заглушка
  23. }
  24.  
  25. static void WithException(int iterations)
  26. {
  27. var watch = Stopwatch.StartNew();
  28.  
  29. try
  30. {
  31. for (int i = 0; i < iterations; i++)
  32. SomeAction();
  33.  
  34. throw new Exception();
  35. }
  36. catch (Exception e)
  37. {
  38. Handle();
  39. }
  40.  
  41. watch.Stop();
  42.  
  43. NumberFormatInfo nfi = (NumberFormatInfo)
  44. CultureInfo.InvariantCulture.NumberFormat.Clone();
  45. nfi.NumberGroupSeparator = " ";
  46.  
  47. Console.WriteLine("with exception {1} iteration {0} ms", watch.ElapsedMilliseconds, iterations.ToString("n0", nfi));
  48. Console.WriteLine();
  49. }
  50.  
  51. static void WithoutException(int iterations)
  52. {
  53. var watch = Stopwatch.StartNew();
  54. for (int i = 0; i < iterations; i++)
  55. SomeAction();
  56.  
  57. Handle();
  58.  
  59. watch.Stop();
  60. NumberFormatInfo nfi = (NumberFormatInfo)
  61. CultureInfo.InvariantCulture.NumberFormat.Clone();
  62. nfi.NumberGroupSeparator = " ";
  63.  
  64. Console.WriteLine("without exception {1} iteration {0} ms", watch.ElapsedMilliseconds, iterations.ToString("n0", nfi));
  65. }
  66.  
  67. static void Main(string[] args)
  68. {
  69. WithoutException(1); // этот и следующий вызов для "прогрева"
  70. WithException(1);
  71.  
  72. WithoutException(1);
  73. WithException(1);
  74.  
  75. WithoutException(1000);
  76. WithException(1000);
  77.  
  78. WithoutException(100000);
  79. WithException(100000);
  80.  
  81. WithoutException(500000);
  82. WithException(500000);
  83.  
  84. Console.ReadLine();
  85. }
  86. }
  87. }
Time limit exceeded #stdin #stdout 5s 29496KB
stdin
Standard input is empty
stdout
without exception 1 iteration 1 ms
with exception 1 iteration 0 ms

without exception 1 iteration 0 ms
with exception 1 iteration 0 ms

without exception 1 000 iteration 30 ms
with exception 1 000 iteration 30 ms

without exception 100 000 iteration 3062 ms