简体   繁体   English

.net事件的性能影响

[英]Performance implications of .net Events

We were having a discussion at the office on how to solve a particular problem and using an event was raised (no pun intended). 我们正在办公室讨论如何解决某个特定问题,并提出了一个事件(没有双关语)。 The response was negative citing misuse and that they can be expensive. 由于滥用行为的反应是负面的,而且价格昂贵。

I understand the concerns behind misuse and I know that they are just a special multicast delegate but given a scenario where there is at most one listener why would using an event over a method call be considered "expensive"? 我理解滥用背后的问题,我知道它们只是一个特殊的多播委托,但考虑到最多只有一个监听器的情况,为什么在方法调用上使用事件被认为是“昂贵的”?

Update: 更新:

Just to be clear this is not about any particular implementation this is a more general question about the cost of using an event over a method call. 需要明确的是,这与任何特定实现无关,这是关于在方法调用上使用事件的成本的更一般性问题。

Test it in your scenario - I would imagine that many, many factors can affect this, especially when we're talking something with such a low relative cost as a method/delegate call. 在你的场景中测试它 - 我会想象很多很多因素都会影响到这一点,特别是当我们谈论一个方法/委托调用相对较低的相对成本时。

A quick and simple test with a release build not under a debugger, compiled under 4.0 as "any cpu", and running on 64-bit windows 7: 一个快速而简单的测试,发布版本不在调试器下,在4.0下编译为“任何cpu”,并在64位Windows 7上运行:

Another Edit : Oops Here's a better result. 另一个编辑 :哎呀这是一个更好的结果。 See the code for how it's working 请参阅代码了解它的工作原理

10000000 direct reference calls     :   0.011 s
10000000 calls through an interface :   0.037 s
10000000 invocations of an event    :   0.067 s
10000000 calls through Action<int>  :   0.035 s

So in a straight "do nothing test" with ten million calls, an event adds .474 sec [ edit , only .026 now on a much better machine, still roughly double however] 所以在一条直线“什么都不做试验”十万个呼叫,事件[但是一个更好的机器上编辑 ,只有0.026现在,仍然大约一倍]增加了0.474秒

I would worry more about correctness of design than half a second every 10 million calls, unless you are expecting to do that many calls in short periods of time (in which case perhaps there is a more fundamental design issue). 我会更担心设计的正确性,而不是每1000万次呼叫半秒,除非你期望在短时间内做那么多次呼叫(在这种情况下,可能存在更基本的设计问题)。

In .NET 2.0 calling a delegate is just as fast as an interface method call. 在.NET 2.0中, 调用委托与接口方法调用一样快 They even seem to be a bit faster. 他们甚至似乎更快一点。

But even if the overhead were 10x higher, I doubt delegate calls would ever be the speed bottleneck in your application. 但即使开销高出10倍,我也怀疑委托调用将成为您应用程序中的速度瓶颈。 Use a profiler if you want to find the real bottlenecks. 如果要查找真正的瓶颈,请使用分析器。

edit in response to the claim that events are slower than delegates: In fact, events are delegates. 编辑以响应事件比委托更慢的声明:实际上,事件委托。 This code shows that their performance is identical, at least on my machine. 此代码表明它们的性能完全相同,至少在我的机器上是这样。

Performance is definitely not something you should concern yourself about. 性能绝对不是你应该关注的。 You're talking about nanoseconds here. 你在这里谈论纳秒。 If you have just one listener, there is no difference at all. 如果你只有一个听众,那就完全没有区别了。

I would simply consider what makes more sense in your application, using events or calling a method and choose the best option. 我只想考虑在您的应用程序中使用事件或调用方法更有意义并选择最佳选项。 The main difference being that an object A generating events doesn't have to know its listener(s). 主要区别在于生成事件的对象A不必知道其侦听器。 The same object A calling a method has to know the instance to call it from. 同样的对象A调用的方法必须知道的情况下,从调用它。 Where do you want to put the dependency? 你想把依赖放在哪里?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM