fork(2) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5.  
  6. namespace ConsoleApplication58
  7. {
  8. internal class Program
  9. {
  10. private class A
  11. {
  12.  
  13. }
  14.  
  15. private static bool F<T>(T a, T b) where T : class
  16. {
  17. return a == b;
  18. }
  19.  
  20. private static bool F2(A a, A b)
  21. {
  22. return a == b;
  23. }
  24.  
  25. private static void Main()
  26. {
  27. const int rounds = 100, n = 10000000;
  28. var a = new A();
  29. var fList = new List<TimeSpan>();
  30. var f2List = new List<TimeSpan>();
  31. for (int i = 0; i < rounds; i++)
  32. {
  33. //test generic
  34. GCClear();
  35. bool res;
  36. var sw = new Stopwatch();
  37. sw.Start();
  38. for (int j = 0; j < n; j++)
  39. {
  40. res = F(a, a);
  41. }
  42. sw.Stop();
  43. fList.Add(sw.Elapsed);
  44.  
  45. //test not-generic
  46. GCClear();
  47. bool res2;
  48. var sw2 = new Stopwatch();
  49. sw2.Start();
  50. for (int j = 0; j < n; j++)
  51. {
  52. res = F2(a, a);
  53. }
  54. sw2.Stop();
  55. f2List.Add(sw2.Elapsed);
  56. }
  57. double f1AverageTicks = fList.Average(ts => ts.Ticks);
  58. Console.WriteLine("Elapsed for F = {0} \t ticks = {1}", fList.Average(ts => ts.TotalMilliseconds),
  59. f1AverageTicks);
  60. double f2AverageTicks = f2List.Average(ts => ts.Ticks);
  61. Console.WriteLine("Elapsed for F2 = {0} \t ticks = {1}", f2List.Average(ts => ts.TotalMilliseconds),
  62. f2AverageTicks);
  63. Console.WriteLine("Not-generic method is {0} times faster, or on {1}%", f1AverageTicks/f2AverageTicks,
  64. (f1AverageTicks/f2AverageTicks - 1)*100);
  65. Console.ReadKey();
  66. }
  67.  
  68. private static void GCClear()
  69. {
  70. GC.Collect();
  71. GC.WaitForPendingFinalizers();
  72. GC.Collect();
  73. }
  74. }
  75. }
  76.  
Success #stdin #stdout 1.83s 34016KB
stdin
Standard input is empty
stdout
Elapsed for F = 8.771168 	 ticks = 87711.68
Elapsed for F2 = 8.781697 	 ticks = 87816.97
Not-generic method is 0.998801029003847 times faster, or on -0.119897099615263%