Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using delegates slow down my .NET programs?

Tags:

.net

delegates

Does using delegates slow down my programs?

I've been avoiding them because I really have no clue if they make my programs any slower. I know if I cause a (catch) exception, that uses quite a bit of CPU power but I don't know about Delegates and Events and what .NET does to them.

like image 615
danmine Avatar asked Nov 20 '08 09:11

danmine


People also ask

Are delegates slow?

Delegates are too damn slow; Delegates are faster because they are only a pointer to a method. Interfaces need to use a v-table to then find a delegate; They are equal, but delegates are easier to use.

What is the advantage of using delegates in C#?

Advantages to using them in design:Allow you to develop libraries and classes that are easily extensible, since it provides an easy way to hook in other functionality (for example, a where clause in LINQ can use a delegate [Func<T,bool>] to filter on, without having to write new code in the Where method.

What is advantage of using delegates?

Benefits of DelegatingGives you the time and ability to focus on higher-level tasks. Gives others the ability to learn and develop new skills. Develops trust between workers and improves communication. Improves efficiency, productivity, and time management.


1 Answers

Delegates are very, very fast. Not quite as fast as direct method calls, but not far off. The chances of them becoming a bottleneck are miniscule.

(Likewise exceptions, when used properly, rarely actually cause a performance issue.)

Would using delegates make your code simpler, more readable, and more robust? If so, use them. Measure your performance carefully, and keep an eye on it. Move away from readability for the sake of performance only when the data is clear.

I'm sure there are some graphs around showing the speed of delegates vs interfaces vs non-virtual method calls etc - I don't know where they are, but you could always run tests yourself if you're really worried.

like image 113
Jon Skeet Avatar answered Sep 21 '22 15:09

Jon Skeet