当前位置:网站首页>Performance of calling delegates vs methods

Performance of calling delegates vs methods

2022-07-05 03:03:00 fyin1314

problem :

Following this question - Pass Method as Parameter using C# and some of my personal experience I'd like to know a little more about the performance of calling a delegate vs just calling a method in C#. The next question - Use C# Pass the method as an argument , And some of my personal experience , I want to know more about the performance of invoking delegates rather than just using C# Performance of calling methods .

Although delegates are extremely convenient, I had an app that did lots of callbacks via delegates and when we rewrote this to use callback interfaces we got an order of magnitude speed improvement. Although delegation is very convenient , But I have an application that makes many callbacks through delegates , When we rewrite the program to use the callback interface , The speed has been improved .This was with .NET 2.0 so I'm not sure how things have changed with 3 and 4. This is a .NET 2.0 Version of , So I'm not sure 3 and 4 The change of .

How are calls to delegates handled internally in the compiler/CLR and how does this affect performance of method calls? How to use the compiler / CLR Internal processing calls to delegates , How does this affect the performance of method calls ?


EDIT - To clarify what I mean by delegates vs callback interfaces. edit - To clarify, I mean delegate and callback interfaces .

For asynchronous calls my class could provide an OnComplete event and associated delegate which the caller could subscribe to. For asynchronous calls , My class can provide OnComplete Events and associated delegates that the caller can subscribe to .

Alternatively I could create an ICallback interface with an OnComplete method that the caller implements and then registers itself with the class that will then call that method on completion (ie the way Java handles these things). perhaps , I can implement it with the caller OnComplete Method to create a ICallback Interface , Then register yourself with this class , This class will call this method when it is finished ( namely Java The way to deal with these things ).


Solution :

Reference resources : https://stackoom.com/en/question/8joV
原网站

版权声明
本文为[fyin1314]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140825303551.html