Last active
August 7, 2019 11:29
-
-
Save didii/c4e8ef021fb8b9fca7898d71eb0de79a to your computer and use it in GitHub Desktop.
Event comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace FluxSwiss.Auctioning.Intranet.UnitTests | |
{ | |
[TestClass] | |
public class Test | |
{ | |
public event EventHandler<string> NonEmptyEvent = delegate { }; | |
public event EventHandler<string> EmptyEvent; | |
private int count = 70000000; | |
// Execution time: ~271ms | |
[TestMethod] | |
public void NonEmptyEventTest() | |
{ | |
for (var i = 0; i < count; i++) | |
NonEmptyEvent(this, "some data"); | |
} | |
// Execution time: ~191ms | |
[TestMethod] | |
public void EmptyEventTest() | |
{ | |
for (var i = 0; i < count; i++) | |
EmptyEvent?.Invoke(this, "some data"); | |
} | |
// Execution time: ~969ms | |
[TestMethod] | |
public void NonEmptyEventTest_WithSingleSubscription() | |
{ | |
// Subscribe | |
var variable = string.Empty; | |
NonEmptyEvent += (sender, args) => | |
{ | |
if (sender == this) | |
{ | |
variable = args; | |
} | |
}; | |
NonEmptyEventTest(); | |
} | |
// Execution time: ~483ms | |
[TestMethod] | |
public void EmptyEventTest_WithSingleSubscription() | |
{ | |
// Subscribe | |
var variable = string.Empty; | |
EmptyEvent += (sender, args) => | |
{ | |
if (sender == this) | |
{ | |
variable = args; | |
} | |
}; | |
EmptyEventTest(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment