fork(1) download
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. static class SClass
  5. {
  6. public static void StatClassCall(ref long used_var)
  7. {
  8. used_var++;
  9. }
  10. }
  11.  
  12. internal interface IProgram
  13. {
  14. void Interface(ref long used_var);
  15. }
  16.  
  17. internal class Base
  18. {
  19. public virtual void Virtual(ref long used_var)
  20. {
  21. used_var += 2;
  22. }
  23. }
  24.  
  25. internal class Program : Base, IProgram
  26. {
  27. const long N = 100500000;
  28. static void Static(ref long used_var)
  29. {
  30. used_var++;
  31. }
  32. void NonStatic(ref long used_var)
  33. {
  34. used_var++;
  35. }
  36. public override void Virtual(ref long used_var)
  37. {
  38. used_var++;
  39. }
  40.  
  41. public void Interface(ref long used_var)
  42. {
  43. used_var++;
  44. }
  45.  
  46. private static void Main(string[] args)
  47. {
  48. var prog = new Program();
  49. long result = N;
  50. SClass.StatClassCall(ref result); // create class
  51.  
  52. Console.WriteLine("Not static: {0}", prog.NonStaticCall(ref result));
  53. Console.WriteLine("Virtual: {0}", prog.VirtCall(ref result));
  54. Console.WriteLine("Interface Call: {0}", prog.InterfaceCall(ref result));
  55. Console.WriteLine("Static: {0}", prog.StaticCall(ref result));
  56. Console.WriteLine("NNNs - {0}, {1}", result, 0);
  57. //Console.ReadKey();
  58. }
  59.  
  60. TimeSpan StaticCall(ref long res)
  61. {
  62. var sw = new Stopwatch();
  63. sw.Start();
  64. for (var i = N; i > 0; i--)
  65. Static(ref res);
  66. sw.Stop();
  67. return sw.Elapsed;
  68. }
  69.  
  70. TimeSpan NonStaticCall(ref long res)
  71. {
  72. var sw = new Stopwatch();
  73. sw.Start();
  74. for (var i = N; i > 0; i--)
  75. NonStatic(ref res);
  76. sw.Stop();
  77. return sw.Elapsed;
  78. }
  79.  
  80. TimeSpan VirtCall(ref long res)
  81. {
  82. var sw = new Stopwatch();
  83. sw.Start();
  84. for (var i = N; i > 0; i--)
  85. Virtual(ref res);
  86. sw.Stop();
  87. return sw.Elapsed;
  88. }
  89.  
  90. TimeSpan StatClassCall(ref long res)
  91. {
  92. var sw = new Stopwatch();
  93. sw.Start();
  94. for (var i = N; i > 0; i--)
  95. SClass.StatClassCall(ref res);
  96. sw.Stop();
  97. return sw.Elapsed;
  98. }
  99.  
  100. TimeSpan InterfaceCall(ref long res)
  101. {
  102. var sw = new Stopwatch();
  103. sw.Start();
  104. for (var i = N; i > 0; i--)
  105. Interface(ref res);
  106. sw.Stop();
  107. return sw.Elapsed;
  108. }
  109.  
  110. }
Success #stdin #stdout 0.76s 131520KB
stdin
Standard input is empty
stdout
Not static: 00:00:00.1811481
Virtual: 00:00:00.2084212
Interface Call: 00:00:00.1812678
Static:     00:00:00.1848647
NNNs - 502500001, 0