Rob Eisenberg posted a pretty clever trick on how to avoid checking if the event delegate is null before using it. I decided to try it out and see if there was any performance penalty for calling an empty function versus checking for null. At least for me the empty function approach was slower but I really don't think this matters unless you have some really time critical piece of code. I will be using the empty function trick henceforth.
The code I used to perform the test was:
Running these tests two times, the first without subscribing to the events and the with subscribing to the events.
My results:
Without subscribing:
With subscribing:
The code I used to perform the test was:
class EventTimeTest
{
public event EventHandler StandardEvent;
public event EventHandler TrickyEvent = delegate { };
public void Test()
{
Stopwatch timer;
EventArgs e = new EventArgs();
for (int testCase = 0; testCase < 9; testCase++)
{
int limit = (int)Math.Pow(10, testCase);
timer = new Stopwatch();
timer.Start();
for (int iteration = 0; iteration < limit; iteration++)
{
OnStandardEvent(e);
}
timer.Stop();
double standardMs = timer.ElapsedMilliseconds;
double standardTicks = timer.ElapsedTicks;
timer = new Stopwatch();
timer.Start();
for (int iteration = 0; iteration < limit; iteration++)
{
OnTrickyEvent(e);
}
timer.Stop();
double msDifference = standardMs != 0 ? timer.ElapsedMilliseconds/standardMs : 0;
double tickDifference = standardTicks != 0 ? timer.ElapsedTicks/standardTicks : 0;
Console.WriteLine("{0}: MS Standard: {1} Tricky: {2} Difference {3}",
limit, standardMs, timer.ElapsedMilliseconds, msDifference);
Console.WriteLine("{0}: Ticks Standard: {1} Tricky: {2} Difference {3}",
limit, standardTicks, timer.ElapsedTicks, tickDifference);
}
}
private void OnStandardEvent(EventArgs e)
{
if (StandardEvent != null)
StandardEvent(this, e);
}
private void OnTrickyEvent(EventArgs e)
{
TrickyEvent(this, e);
}
}
Running these tests two times, the first without subscribing to the events and the with subscribing to the events.
My results:
Without subscribing:
1: MS Standard: 0 Tricky: 0 Difference 0
1: Ticks Standard: 334528 Tricky: 414792 Difference 1,23993208341305
10: MS Standard: 0 Tricky: 0 Difference 0
10: Ticks Standard: 4288 Tricky: 6840 Difference 1,59514925373134
100: MS Standard: 0 Tricky: 0 Difference 0
100: Ticks Standard: 3984 Tricky: 5864 Difference 1,4718875502008
1000: MS Standard: 0 Tricky: 0 Difference 0
1000: Ticks Standard: 23160 Tricky: 41544 Difference 1,79378238341969
10000: MS Standard: 0 Tricky: 0 Difference 0
10000: Ticks Standard: 208584 Tricky: 397904 Difference 1,90764392283205
100000: MS Standard: 0 Tricky: 1 Difference 0
100000: Ticks Standard: 2063880 Tricky: 4035024 Difference 1,95506715506716
1000000: MS Standard: 8 Tricky: 15 Difference 1,875
1000000: Ticks Standard: 21348944 Tricky: 42312056 Difference 1,98192734966189
10000000: MS Standard: 81 Tricky: 151 Difference 1,8641975308642
10000000: Ticks Standard: 217051903 Tricky: 403840296 Difference 1,86057016970729
100000000: MS Standard: 813 Tricky: 1530 Difference 1,88191881918819
100000000: Ticks Standard: 2166929311 Tricky: 4074169320 Difference 1,8801579263884
With subscribing:
1: MS Standard: 0 Tricky: 0 Difference 0
1: Ticks Standard: 196896 Tricky: 231644 Difference 1,17647895335609
10: MS Standard: 0 Tricky: 0 Difference 0
10: Ticks Standard: 2472 Tricky: 3192 Difference 1,29126213592233
100: MS Standard: 0 Tricky: 0 Difference 0
100: Ticks Standard: 6408 Tricky: 12120 Difference 1,89138576779026
1000: MS Standard: 0 Tricky: 0 Difference 0
1000: Ticks Standard: 46048 Tricky: 100264 Difference 2,17738012508687
10000: MS Standard: 0 Tricky: 0 Difference 0
10000: Ticks Standard: 442368 Tricky: 2101134 Difference 4,74974229600694
100000: MS Standard: 1 Tricky: 3 Difference 3
100000: Ticks Standard: 4443286 Tricky: 9994829 Difference 2,24942283706248
1000000: MS Standard: 17 Tricky: 38 Difference 2,23529411764706
1000000: Ticks Standard: 45348919 Tricky: 102265594 Difference 2,25508339019062
10000000: MS Standard: 170 Tricky: 379 Difference 2,22941176470588
10000000: Ticks Standard: 455012748 Tricky: 1010235251 Difference 2,22023504932657
100000000: MS Standard: 1711 Tricky: 3849 Difference 2,24956165984804
100000000: Ticks Standard: 4555073376 Tricky: 10247927123 Difference 2,24978310492094