Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :))

like image 252
Jonathan C Dickinson Avatar asked Jun 30 '26 23:06

Jonathan C Dickinson


2 Answers

Events firing are delegate invocations, which are a a bit slower than virtual calls

alt text
(source: microsoft.com)

But dealing with interfaces for subscriber/publisher/observer/observable scenario is more painful that using events.

like image 93
Romain Verdier Avatar answered Jul 03 '26 13:07

Romain Verdier


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?

like image 44
Jon Skeet Avatar answered Jul 03 '26 13:07

Jon Skeet