Asynchronous operations performance

让人想犯罪 __ 提交于 2020-02-14 02:25:27

问题


One of the features of asynchronous programming in .NET is saving threads during long running operation execution. The FileStream class can be setup to allow asynchronous operations, that allows running (e.g.) a copy operation without virtually using any threads. To my surprise, I found that running asynchronous stream copy performs not only slower, but also uses more processing power than synchronous stream copy equivalent.

Is there any benchmark tests were done to compare a synchronous vs asynchronous operation execution (file, network, etc.)? Does it really make sense to perform an asynchronous operation instead of spanning separate thread and perform synchronous operation in server environment if the asynchronous operation is times slower than the synchronous one?


回答1:


Actually, the conditions under which file i/o will be actually be asynchronous are rather specific, even at the native Win32 level. See this article for more details.




回答2:


Quoting from the article you included in your comment to @Alex's response.

In situations where an I/O request is expected to take a large amount of time, such as a refresh or backup of a large database or a slow communications link, asynchronous I/O is generally a good way to optimize processing efficiency. However, for relatively fast I/O operations, the overhead of processing kernel I/O requests and kernel signals may make asynchronous I/O less beneficial, particularly if many fast I/O operations need to be made. In this case, synchronous I/O would be better. The mechanisms and implementation details of how to accomplish these tasks vary depending on the type of device handle that is used and the particular needs of the application. In other words, there are usually multiple ways to solve the problem.

FWIW, I think @Alex is correct that there is another thread running the kernel code associated with your I/O request. That thread is not managed by your application, though. The thread running the kernel code may itself block on an device I/O request and wait for the actual hardware to complete the request before signaling your user-mode thread, but it is still there nonetheless.

Using asynchronous threads shouldn't be thought of as a way to increase the speed of any particular request, but rather a way to increase the overall efficiency by allowing your application to continue processing on other tasks while awaiting relatively slow I/O.




回答3:


Are you sure that you benchmarked your copy operation correctly? Any asynchronous operation just create new thread and runs the operation in new thread while leaving main thread to do other things.

The asynchronous operations mostly give a a few simplifications(less lines of code) over creating the thread yourself but they should not impact performance more then creating a new Thread.



来源:https://stackoverflow.com/questions/575974/asynchronous-operations-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!