Using delegates slows down my .NET programs?

Do delegates use slowdowns in my programs?

I avoided them because I really don't understand if they will slow down my programs. I know if I can throw an exception that uses quite a lot of processor power, but I don’t know about delegates and events and what .NET does with them.

+16
delegates
Nov 20 '08 at 9:29
source share
4 answers

Delegates are very, very fast. Not as fast as direct method calls, but not far off. The chances of becoming bottlenecks are slim.

(Likewise, when used properly , performance problems rarely occur .)

Will using delegates make your code simpler, more understandable, and more reliable? If so, use them. Carefully measure your performance and monitor it. Deviate from readability for performance only when the data becomes clear.

I am sure that there are some graphs around showing the speed of delegation vs interfaces against calls to non-virtual methods, etc. β€œI don’t know where they are, but you can always run the tests yourself if you are really worried.”

+16
Nov 20 '08 at 9:32
source share

Just a small addition to the Jon post: when used in normal C # mode (i.e. via lambdas / anonymous methods / event handlers / etc), they are certainly very fast, but note that another important use of delegates can be execute dynamic code (either methods created at runtime, or existing methods through reflection, and Delegate.CreateDelegate). When used in this second way, delegates offer a very significant speed improvement (compared to Invoke reflection, etc.).

So feel free to use delegates. And, in particular, if you have doubts, measure it for a realistic code: it makes no sense whether 1 or 100 micro-pettles * are required, if this only makes up 0.01% of your total execution time.

* = just some arbitrarily small time ...

+9
Nov 20 '08 at 9:41
source share

Performance can be a daunting subject when it comes to programming. For example, some people are absolutely adamant that boxing is the root of all evil. Other people think that string concats are a great success.

In reality, everything is relative, and it all comes down to the context in which you are speaking. If you are programming on a mobile device, you will need to optimize more than if you were working on a desktop application.

This usually comes down to a trade-off between code efficiency and elegance. Let's say you created the most amazingly elegant, convenient, and understandable code base in the world. As soon as we launch some performance optimizations, we start code clouds with some, possibly opposing, intuitive, very specialized things. If we went to the city for optimization, we could make a performance saving, for example, 5 or 10 percent, but in the process completely destroy the elegance of the code.

Question: "Is it worth it?".

If performance is absolutely critical to your project, then run the profiler on your code. If you find that 90% of your CPU time is consumed using a particularly inefficient method, then this method is a good candidate for optimization. This is usually not worth pursuing the benefits of poor performance if you are not working with a performance-critical application.

+6
Nov 20 '08 at 11:32
source share

I am working on Windows CE, so this kind of thing is sometimes a little more appropriate. For example, a cheeky reflection application can really hurt, so we tend to avoid reflection where reasonable (obviously, small reflection applications are great). Obviously, I am not doing such madness on the desktop.

I heard people mumble about delegates and CE performance, but as far as I'm concerned about its complete guff. I heard that it "slows down the method call by 30%," but if it's 30% of a crappy algorithm, then whose error is it? Another slowdown in CE is virtual methods, since there is no lookup table, it manually processes them for the first time and caches the result. This means that if you miss all your memory, these caches will be cleared, and this will lead to the next time the performance is reached. But what is considered to be if you should throw away OOP's useful skills for performance?

I find that many of these "OMG don't use too slow" are just excuses. Mostly excuses, because people don’t know what is really wrong with their application, and it’s easy to blame it on the internal workings of the CLR, not their own code. If your performance sucks, I would consider that 99.9% of the time, you can change something in your part of the application or design, which does not throw away tools and does not give much better improvement.

+6
Nov 20 '08 at 11:59
source share



All Articles