Đối với bất kỳ ai quan tâm đến hiệu suất của cuộc gọi sự kiện, tôi đã thực hiện điểm chuẩn đơn giản này. Nó cho thấy sự khác biệt giữa việc gọi một phương thức trực tiếp, gọi nó qua giao diện, thông qua đại biểu và thông qua sự kiện, trong đó một trình xử lý được đính kèm.
Trong mỗi trường hợp, phương pháp được gọi theo cách tương ứng 1 000 000 000 lần. Dưới đây là (có thể ngạc nhiên) kết quả:
Đại biểu gọi: 23 240 ms - nhanh nhất gọi
tổ chức sự kiện: 23 295 ms
cuộc gọi trực tiếp: 23 396 ms
Giao diện cuộc gọi: 23 716 ms - chậm nhất
Các mesurements được thực hiện trong bản phát hành xây dựng bằng C# trong .NET4.0.
Mã này là ở đây:
class Program
{
static void Main(string[] args)
{
TestClass.RunTest();
Console.ReadLine();
}
}
interface ITestClass
{
void TestMethod(object sender, TestEventArgs eventErgs);
}
class TestClass : ITestClass
{
#region Events
event EventHandler<TestEventArgs> TestEvent;
#endregion
#region Constructor
public TestClass()
{
TestEvent += TestMethod;
}
#endregion
#region Public Methods
public static void RunTest()
{
int testCount = 1000000000; //1 000 000 000
string format = "{0:### ### ### ##0}";
#region Direct Call
Console.WriteLine("Direct call");
TestClass testClass = new TestClass();
testClass.TestMethod(testClass, new TestEventArgs(3));
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < testCount; ++i)
{
testClass.TestMethod(testClass, new TestEventArgs(3));
}
stopwatch.Stop();
Console.WriteLine(string.Format(format, stopwatch.ElapsedMilliseconds));
Console.WriteLine();
#endregion
#region Interface Call
Console.WriteLine("Interface call");
ITestClass itestClass = new TestClass();
itestClass.TestMethod(testClass, new TestEventArgs(3));
stopwatch = Stopwatch.StartNew();
for (int i = 0; i < testCount; ++i)
{
itestClass.TestMethod(testClass, new TestEventArgs(3));
}
stopwatch.Stop();
Console.WriteLine(string.Format(format, stopwatch.ElapsedMilliseconds));
Console.WriteLine();
#endregion
#region Delegate Call
Console.WriteLine("Delegate call");
TestClass delegateTestClass = new TestClass();
Action<object, TestEventArgs> delegateMethod = delegateTestClass.TestMethod;
delegateMethod(testClass, new TestEventArgs(3));
stopwatch = Stopwatch.StartNew();
for (int i = 0; i < testCount; ++i)
{
delegateMethod(testClass, new TestEventArgs(3));
}
stopwatch.Stop();
Console.WriteLine(string.Format(format, stopwatch.ElapsedMilliseconds));
Console.WriteLine();
#endregion
#region Event Call
Console.WriteLine("Event call");
TestClass eventTestClast = new TestClass();
eventTestClast.TestEvent(testClass, new TestEventArgs(3));
stopwatch = Stopwatch.StartNew();
for (int i = 0; i < testCount; ++i)
{
eventTestClast.TestEvent(testClass, new TestEventArgs(3));
}
stopwatch.Stop();
Console.WriteLine(string.Format(format, stopwatch.ElapsedMilliseconds));
Console.WriteLine();
#endregion
}
#endregion
#region ITestClass Members
public void TestMethod(object sender, TestEventArgs e)
{
e.Result = e.Value * 3;
}
#endregion
}
class TestEventArgs : EventArgs
{
public int Value { get; private set; }
public int Result { get; set; }
public TestEventArgs(int value)
{
Value = value;
}
}
Đó là nó! Cảm ơn. – dotnetdev