Created
January 8, 2018 14:43
-
-
Save KumoKairo/be74d1f5cbbf5e176d117ed0c1ce52c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
[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