Skip to content

Instantly share code, notes, and snippets.

@didii
Last active August 7, 2019 11:29
Show Gist options
  • Save didii/c4e8ef021fb8b9fca7898d71eb0de79a to your computer and use it in GitHub Desktop.
Save didii/c4e8ef021fb8b9fca7898d71eb0de79a to your computer and use it in GitHub Desktop.
Event comparison
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