I am new to the delegates concept. I've learnt it is similar to pointers in C++. In its advantages, it mentioned effective use of delegates improves the performance.
我是代表们的新概念。我已经知道它类似于C ++中的指针。它的优点是,它提到了代表的有效使用提高了性能。
Considering it's a pointer. How does it improve the performance of an application?
考虑到它是一个指针。它如何改善应用程序的性能?
If anybody could explain this with a simple example, that would be helpful.
如果有人能用一个简单的例子来解释这一点,那将会有所帮助。
3 个解决方案
#1
4
Delegates aren't directly about improving performance - they are about abstracting invocation. In C++ terms, it is indeed like a method pointer.
代表们并不直接关于改进性能 - 它们是关于抽象调用的。在C ++术语中,它确实像一个方法指针。
Most uses of delegates are not related to performance. Let's be clear about that from the outset.
代表的大多数使用与性能无关。让我们从一开始就明确这一点。
However, one main time this can be used to help with performance is that this allows for scenarios like meta-programming. The code (usually library code) can construct complex chains of behaviours based on configuration information at runtime, and then compile that information into a method via any of Expression
, TypeBuilder
or DynamicMethod
(or basically any API that lets you construct IL). But to invoke such a dynamically generated method, you need a delegate - because your static IL that was compiled from C# can't refer to a method that didn't exist at the time.
但是,这可以用来帮助提高性能的一个主要时间是允许元编程之类的场景。代码(通常是库代码)可以在运行时基于配置信息构建复杂的行为链,然后通过Expression,TypeBuilder或DynamicMethod(或基本上任何允许您构造IL的API)将该信息编译到方法中。但是要调用这样一个动态生成的方法,您需要一个委托 - 因为从C#编译的静态IL不能引用当时不存在的方法。
Note that an alternative way to do this would be to use TypeBuilder
to create (at runtime) a new type that inherits from a subclass or implements a known interface, then create an instance of the dynamically generated type, which can be cast to the expected API in the usual manner and invoked normally.
请注意,执行此操作的另一种方法是使用TypeBuilder创建(在运行时)从子类继承的新类型或实现已知接口,然后创建动态生成类型的实例,该实例可以强制转换为预期API以通常的方式正常调用。
#2
2
Please see these links:
请看这些链接:
Performance of calling delegates vs methods
调用代理与方法的性能
Improving performance with generic delegates in .NET
使用.NET中的通用委托提高性能
#3
2
Delegates do not have an significant positive or negative impact on the performance of your application. What they provide is a means of decoupling aspects of your application from each other.
代表对您的申请表现没有重大的正面或负面影响。它们提供的是将应用程序的各个方面相互分离的方法。
Lets say you have a situation where class A
calls B.foo()
. A
is now partially coupled to B
. You might then have a situation where B
needs to call A.bar()
. You now risk tightly coupling the two together. If instead of exposing A
to B
, you instead provide bar
as a delegate, then you have removed that coupling.
假设您遇到A类调用B.foo()的情况。 A现在部分耦合到B.您可能会遇到B需要调用A.bar()的情况。你现在冒险将两者紧密结合在一起。如果不是将A暴露给B,而是将bar作为委托提供,那么您已经删除了该耦合。
#1
4
Delegates aren't directly about improving performance - they are about abstracting invocation. In C++ terms, it is indeed like a method pointer.
代表们并不直接关于改进性能 - 它们是关于抽象调用的。在C ++术语中,它确实像一个方法指针。
Most uses of delegates are not related to performance. Let's be clear about that from the outset.
代表的大多数使用与性能无关。让我们从一开始就明确这一点。
However, one main time this can be used to help with performance is that this allows for scenarios like meta-programming. The code (usually library code) can construct complex chains of behaviours based on configuration information at runtime, and then compile that information into a method via any of Expression
, TypeBuilder
or DynamicMethod
(or basically any API that lets you construct IL). But to invoke such a dynamically generated method, you need a delegate - because your static IL that was compiled from C# can't refer to a method that didn't exist at the time.
但是,这可以用来帮助提高性能的一个主要时间是允许元编程之类的场景。代码(通常是库代码)可以在运行时基于配置信息构建复杂的行为链,然后通过Expression,TypeBuilder或DynamicMethod(或基本上任何允许您构造IL的API)将该信息编译到方法中。但是要调用这样一个动态生成的方法,您需要一个委托 - 因为从C#编译的静态IL不能引用当时不存在的方法。
Note that an alternative way to do this would be to use TypeBuilder
to create (at runtime) a new type that inherits from a subclass or implements a known interface, then create an instance of the dynamically generated type, which can be cast to the expected API in the usual manner and invoked normally.
请注意,执行此操作的另一种方法是使用TypeBuilder创建(在运行时)从子类继承的新类型或实现已知接口,然后创建动态生成类型的实例,该实例可以强制转换为预期API以通常的方式正常调用。
#2
2
Please see these links:
请看这些链接:
Performance of calling delegates vs methods
调用代理与方法的性能
Improving performance with generic delegates in .NET
使用.NET中的通用委托提高性能
#3
2
Delegates do not have an significant positive or negative impact on the performance of your application. What they provide is a means of decoupling aspects of your application from each other.
代表对您的申请表现没有重大的正面或负面影响。它们提供的是将应用程序的各个方面相互分离的方法。
Lets say you have a situation where class A
calls B.foo()
. A
is now partially coupled to B
. You might then have a situation where B
needs to call A.bar()
. You now risk tightly coupling the two together. If instead of exposing A
to B
, you instead provide bar
as a delegate, then you have removed that coupling.
假设您遇到A类调用B.foo()的情况。 A现在部分耦合到B.您可能会遇到B需要调用A.bar()的情况。你现在冒险将两者紧密结合在一起。如果不是将A暴露给B,而是将bar作为委托提供,那么您已经删除了该耦合。