关于委托功能 [英] About delegate functionality

查看:82
本文介绍了关于委托功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我已经处理过委托,并且知道它们充当函数的指针,并且所有成员初始化部分和函数初始化部分都是在委托对象实际调用函数之前完成的,这实际上使执行起来更快.例如将数据绑定到网格,填充下拉列表等.

我的问题是,如果在另一个函数内部创建了一个函数,并且如果我在委托的帮助下同时调用了两个函数,那么实际上正在执行该操作的外部函数的执行是否会更快.

例如,

公共无效方法()
{
----------------
----------------
一些代码
method1();
}

现在,如果我使用委托调用 method()函数,并使用相同的委托调用内部函数( method1()),则函数 method()会更快.

谢谢

Varun Sareen

Dear Friends,

I have worked on the delegates and know that they act as pointer to functions and all the members initialization part and function initialization part is done before actually the function is called by delegate object which actually makes it faster to execute. like binding data to grid, filling drop-down etc.

My question here is that if there is one function which is made inside another function and if i call both the function with the help of delegate then will the execution of outer function which is actually doing the action will be faster or not.

e.g.,

public void method()
{
----------------
----------------
some code
method1();
}

now if i am calling the method() function with delegate and also calling the inner function (method1()) with the same delegate then will the execution of the function method() will be more faster as comparison to if i don''t call the inner function through delegate.

Thanks

Varun Sareen

推荐答案

首先,您对代表的理解至少是幼稚的.委托不充当函数的指针.首先,.NET中没有指针(除非我们谈论不安全的代码),有些引用是不一样的.一个委托实例确实包含一个指向该方法的指针,但也包含对该类/结构实例"this"的引用.另外,委托实例是某个类的实例,该类带有一个完整的容器,这些容器称为指针",称为调用列表.

我的文章动态方法分派器 [在调用内部隐藏临时方法方法的主体 [> http://en.wikipedia.org/wiki/Closure_(computer_science) [ ^ ].

在过去的解决方案中,我提供了自己的代码示例和一些解释:adjustMargins是您的内部方法,第一个示例中的AdjustMargins是同一方法的常规"(不是内部)变体.

具有内部方法"的变体不能比常规"方法更快,因为使用内部方法"的方法首先要创建委托的实例,然后才调用(调用)委托实例.当然有一些开销.我认为对于大多数应用程序而言,性能损失将是微不足道的.

当您说比起我不通过委托来调用内部函数要快得多"时,您可能并不意味着根本就不调用它,而是调用同一方法的常规"版本,因为当然不是打电话总是比打电话更快. :-)

您可以轻松测试性能.只需使用类System.Diagnostics.Stopwatch,请参见MSDN帮助页面和代码示例:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx [
First of all, your understanding of delegates is at least naive. A delegate does not act as a pointer to a function. First of all, there are no pointers in .NET (unless we talk unsafe code), there are references which is not the same; and a delegate instance does hold a pointer to the method, but also a reference to the instance of the class/structure, "this". Also, a delegate instance is an instance of some class which carries a whole container of such "pointers" called invocation list.

This is explained in my article Dynamic Method Dispatcher[^].

Now, let''s sort out the issues with "inner function". There are no inner methods in C#, but there is a way to mimic such thing using delegates. This is explained in my Tips & Tricks article Hide Ad-hoc Methods Inside the Calling Method’s Body[^].



Using inner functions in general and their implementations in the form of anonymous delegates opens up a way to such an interesting phenomenon as closure.

Please see http://en.wikipedia.org/wiki/Closure_(computer_science)[^].

In my past solution, I provided my own code sample and some explanations: Looping for creating new thread give some problem[^].

[END EDIT]

Please look at the code samples in the article referenced above. There, AdjustLayout is you "outer method", adjustMargins is your inner method, and AdjustMargins in first sample is a "regular" (not inner) variant of the same method.

A variant with "inner method" cannot be faster then a "regular" approach, because with "inner method" approach you first create an instance of a delegate and only after that invoke (call) the delegate instance. It certainly have some overhead. I think the performance loss will be insignificant for most applications.

When you say "faster as comparison to if I don''t call the inner function through delegate", you probably don''t mean not calling it at all but calling a "regular" version of the same method, because of course not calling anything is always faster than calling something. :-)

You can easily test performance. Just use the class System.Diagnostics.Stopwatch, see MSDN help page and code sample: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^]. If you do, please share your observations.

—SA


瓦伦,
代理性能 [
Hi varun,
Delegate Performance[^]


这篇关于关于委托功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆