views:

409

answers:

5
+2  Q: 

Events in C#

Just how much slower are events? I have written a streaming XML parser (that can handle open-ended and incomplete documents) and by tearing out the events and using an interface instead I got a significant speed boost.

Does any one else have any war stories?

(Let's not open the GC can of worms here, we all know it's broken :))

+4  A: 

Events are really just delegates. From what I recall, they were made much faster in the 2.0 CLR. I'm surprised that replacing events with an interface made your code significantly faster - in my experience they're pretty fast, and if you're dealing with XML, I wouldn't have expected the event calls to be the bottlenecks.

Did your code constantly subscribe to and unsubscribe from events? Do you have any indication of the number of event calls that were made when parsing a particular document?

Jon Skeet
"I'm surprised that events made your code significantly faster" - you mean slower ;)
VVS
No, there is one XML parser per client. Xml calls per document can range to the millions, it's an IM protocol.
Jonathan C Dickinson
I meant "surprised that moving away from events made your code significantly faster" - edited, thanks :)
Jon Skeet
On the bottlenecks, you are completely right. It's one of my gripes with XMPP, but I have authored a binary XML solution (it'ls like EXI, but really concentrates on speed and not size) that can be started (like STARTTLS) by the client at any point.
Jonathan C Dickinson
A: 

If there's no reflection involved in the calls then I assume the overhead is pretty negligible. Assumption could be wrong of course . Do you have a micro benchmark to demonstrate this?

I could try whip one together. As I (think) I said, the speed increase was observational (but significant at that).
Jonathan C Dickinson
A: 

Events are definately slower than a straight function call, i can't tell you exactly how much slower, but significantly. You could also pass delegates around for "middle" ground. The .NET event system uses delegates, but calling a method directly through the delegate vs the whole event system is still faster.

Bob Dizzle
It's really strange. I mean if it runs on delegates why is it slower than using delegates themselves.
Jonathan C Dickinson
A: 

Delegates have a slight overhead compared to virtual method calls because they're lists of methods and can therefore theoretically invoke multiple handlers.

Konrad Rudolph
I am not sure, the reason I used an interface is because there was only ever one subscriber. Which means that the list would have one item. I wonder what is up, maybe I should crack open Reflector and look at the differences.
Jonathan C Dickinson
Even if the list had only one item, iterating over it instead of directly calling a method (even a virtual one) is bound to be slightly slower.
Konrad Rudolph
+19  A: 
Romain Verdier
Event are delegate calls as per your diagram.
leppie
A delegate call is a delegate invocation. The two following calls produce same IL:myEventHandler(this, EventArgs.Empty);myEventHandler.Invoke(this, EventArgs.Empty);
Romain Verdier