Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. // 'Event' type:
  2. delegate void DelMyEvent();
  3. // consumer:
  4. class Consumer
  5. {
  6. Producer _theProducer;
  7. void RegisterForNotification()
  8. {
  9. _theProducer.OnMyEvent = new DelMyEvent(OnMyEvent);
  10. }
  11. void OnMyEvent() { }
  12. }
  13. // producer:
  14. class Producer
  15. {
  16. public DelMyEvent OnMyEvent;
  17. void SendNotification()
  18. {
  19. if( OnMyEvent != null ) OnMyEvent();
  20. }
  21. }
  22.  
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. TestClass.RunTest();
  28. Console.ReadLine();
  29. }
  30. }
  31.  
  32. interface ITestClass
  33. {
  34. void TestMethod(object sender, TestEventArgs eventErgs);
  35. }
  36.  
  37. class TestClass : ITestClass
  38. {
  39. #region Events
  40.  
  41. event EventHandler<TestEventArgs> TestEvent;
  42.  
  43. #endregion
  44.  
  45. #region Constructor
  46.  
  47. public TestClass()
  48. {
  49. TestEvent += TestMethod;
  50. }
  51.  
  52. #endregion
  53.  
  54. #region Public Methods
  55.  
  56. public static void RunTest()
  57. {
  58. int testCount = 1000000000; //1 000 000 000
  59.  
  60. string format = "{0:### ### ### ##0}";
  61.  
  62. #region Direct Call
  63.  
  64. Console.WriteLine("Direct call");
  65. TestClass testClass = new TestClass();
  66.  
  67. testClass.TestMethod(testClass, new TestEventArgs(3));
  68.  
  69. Stopwatch stopwatch = Stopwatch.StartNew();
  70. for (int i = 0; i < testCount; ++i)
  71. {
  72. testClass.TestMethod(testClass, new TestEventArgs(3));
  73. }
  74. stopwatch.Stop();
  75. Console.WriteLine(string.Format(format, stopwatch.ElapsedMilliseconds));
  76. Console.WriteLine();
  77.  
  78. #endregion
  79.  
  80. #region Interface Call
  81.  
  82. Console.WriteLine("Interface call");
  83. ITestClass itestClass = new TestClass();
  84. itestClass.TestMethod(testClass, new TestEventArgs(3));
  85.  
  86. stopwatch = Stopwatch.StartNew();
  87. for (int i = 0; i < testCount; ++i)
  88. {
  89. itestClass.TestMethod(testClass, new TestEventArgs(3));
  90. }
  91. stopwatch.Stop();
  92. Console.WriteLine(string.Format(format, stopwatch.ElapsedMilliseconds));
  93. Console.WriteLine();
  94.  
  95. #endregion
  96.  
  97. #region Delegate Call
  98.  
  99. Console.WriteLine("Delegate call");
  100. TestClass delegateTestClass = new TestClass();
  101. Action<object, TestEventArgs> delegateMethod = delegateTestClass.TestMethod;
  102. delegateMethod(testClass, new TestEventArgs(3));
  103.  
  104. stopwatch = Stopwatch.StartNew();
  105. for (int i = 0; i < testCount; ++i)
  106. {
  107. delegateMethod(testClass, new TestEventArgs(3));
  108. }
  109. stopwatch.Stop();
  110. Console.WriteLine(string.Format(format, stopwatch.ElapsedMilliseconds));
  111. Console.WriteLine();
  112.  
  113. #endregion
  114.  
  115. #region Event Call
  116.  
  117. Console.WriteLine("Event call");
  118. TestClass eventTestClast = new TestClass();
  119. eventTestClast.TestEvent(testClass, new TestEventArgs(3));
  120.  
  121. stopwatch = Stopwatch.StartNew();
  122. for (int i = 0; i < testCount; ++i)
  123. {
  124. eventTestClast.TestEvent(testClass, new TestEventArgs(3));
  125. }
  126. stopwatch.Stop();
  127. Console.WriteLine(string.Format(format, stopwatch.ElapsedMilliseconds));
  128. Console.WriteLine();
  129.  
  130. #endregion
  131. }
  132.  
  133. #endregion
  134.  
  135. #region ITestClass Members
  136.  
  137. public void TestMethod(object sender, TestEventArgs e)
  138. {
  139. e.Result = e.Value * 3;
  140. }
  141.  
  142. #endregion
  143. }
  144.  
  145. class TestEventArgs : EventArgs
  146. {
  147. public int Value { get; private set; }
  148.  
  149. public int Result { get; set; }
  150.  
  151. public TestEventArgs(int value)
  152. {
  153. Value = value;
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement