Skip to content

Instantly share code, notes, and snippets.

@KumoKairo
Created January 8, 2018 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KumoKairo/be74d1f5cbbf5e176d117ed0c1ce52c8 to your computer and use it in GitHub Desktop.
Save KumoKairo/be74d1f5cbbf5e176d117ed0c1ce52c8 to your computer and use it in GitHub Desktop.
[Test]
public void DelegatesVsVanillaSubscriberList()
{
const int count = 10000;
var observerImplementation = new ObserverImplementation();
var sw = new Stopwatch();
sw.Start();
Action action = () => { };
for (int i = 0; i < count; i++)
{
action += observerImplementation.Action;
}
sw.Stop();
Debug.Log("Action list creation " + sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
action();
sw.Stop();
Debug.Log("Action call " + sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
List<IObserver> observers = new List<IObserver>();
observers.Add(new EmptyObserver());
for (int i = 1; i < count + 1; i++)
{
observers.Add(new ObserverImplementation());
}
sw.Stop();
Debug.Log("Observers addition " + sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
foreach (var observer in observers)
{
observer.Action();
}
sw.Stop();
Debug.Log("Observers call " + sw.ElapsedMilliseconds);
}
private interface IObserver
{
void Action();
}
private class EmptyObserver : IObserver
{
public void Action()
{
}
}
private class ObserverImplementation : IObserver
{
public void Action()
{
// Useless work
var integer = 0;
integer += 153;
var floating = integer / 0.3f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment