gpt4 book ai didi

.net - .NET 中套接字的 Begin* 和 *Async 之间是否存在性能差异?

转载 作者:行者123 更新时间:2023-12-03 13:31:59 25 4
gpt4 key购买 nike

我的应用程序需要向大量客户端(1000 秒)快速广播消息并收集结果。

我正在考虑是否使用 开始发送/结束发送 等函数系列,或使用 发送异步 家庭 - 有任何性能差异吗?除了不需要分配 IAsyncResult 的 *Async 系列之外,它们有什么区别?

如果我理解正确,它们都使用 IO 完成端口和标准的 .net 线程池......那么有什么区别?

最佳答案

不同之处仅在于使用的模式。
SendAsync事实上使用 Event-Based Pattern . BeginSendEndSend使用 IAsyncResult pattern .

编辑:我不知道IAsyncResult接口(interface)在 Socket 中实现类,但这里是 document来自 MSDN,它解释了何时实现一种模式或另一种模式。

摘自最后一部分:

While the Event-based Asynchronous Pattern has many benefits under the previously mentioned scenarios, it does have some drawbacks, which you should be aware of if performance is your most important requirement.

There are three scenarios that the event-based pattern does not address as well as the IAsyncResult pattern:

  1. Blocking wait on one IAsyncResult

  2. Blocking wait on many IAsyncResult objects

  3. Polling for completion on the IAsyncResult

You can address these scenarios by using the event-based pattern, but doing so is more cumbersome than using the IAsyncResult pattern.

Developers often use the IAsyncResult pattern for services that typically have very high performance requirements. For example, the polling for completion scenario is a high-performance server technique.

Additionally, the event-based pattern is less efficient than the IAsyncResult pattern because it creates more objects, especially EventArgs, and because it synchronizes across threads.

The following list shows some recommendations to follow if you decide to use the IAsyncResult pattern:

  • Only expose the IAsyncResult pattern when you specifically require support for WaitHandle or IAsyncResult objects.

  • Only expose the IAsyncResult pattern when you have an existing API that uses the IAsyncResult pattern.

  • If you have an existing API based on the IAsyncResult pattern, consider also exposing the event-based pattern in your next release.

  • Only expose IAsyncResult pattern if you have high performance requirements which you have verified cannot be met by the event-based pattern but can be met by the IAsyncResult pattern.

关于.net - .NET 中套接字的 Begin* 和 *Async 之间是否存在性能差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5202608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com