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

标签 .net sockets asynchronous

我的应用程序需要向大量客户端(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/

相关文章:

javascript - 如何在回调函数中访问全局范围变量? [JS]

javascript - 当我清楚地看到页面上存在该元素时,无法确认该元素是否存在

.net - 构造函数调用其他构造函数: any performance issues?

c# - 单元测试自定义 Serilog 接收器

.net - 在 .NET 中使用 SSH 隧道连接到 postgresql

sockets - Nginx 找不到 Unicorn 的 unix 套接字文件(没有这样的文件或目录)

c - 在 C 中使用 select() 和 write_fds 时发送数据的正确方法

c - Unix 域套接字 : Make Latency constant

c# - 在 C# 上使用 C++/CLI 的情况或优缺点是什么

c# - .Net 枚举 winforms 字体样式?