使用async-await可以给您带来任何性能上的好处吗? [英] Can using async-await give you any performance benefits?

查看:592
本文介绍了使用async-await可以给您带来任何性能上的好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我阅读有关 async - await 的内容时,用例示例始终为 一个您不想冻结的用户界面。所有编程书籍/教程都是相同的,或者UI阻塞是我应该知道的 async - await 的唯一情况关于开发者。

Whenever I read about async-await, the use case example is always one where there's a UI that you don't want to freeze. Either all programming books/tutorials are the same or UI blocking is the only case of async-await that I should know about as a developer.

是否有任何示例可以说明如何使用 async - await 发挥算法的性能优势?就像让我们来处理任何经典的编程面试问题一样:

Are there any examples of how one could use async-await to eke out performance benefits in an algorithm? Like let's take any of the classic programming interview questions:


  • 在二叉树中找到最近的共同祖先

  • 给出 a [0] a [1] ,..., a [n-1] 代表以10为底的数字,找到第二个使用相同数字的最高数字

  • 查找两个排序数组的中位数(即中位数,如果要合并它们的话)

  • 给出数字数组 1 2 ,..., n ,其中一个数字丢失,找到丢失的数字

  • 找到最大的2数组中的数字

  • Find the nearest common ancestor in a binary tree
  • Given a[0], a[1], ..., a[n-1] representing digits of a base-10 number, find the next highest number that uses the same digits
  • Find the median of two sorted arrays (i.e. the median number if you were to merge them)
  • Given an array of numbers 1, 2, ..., n with one number missing, find the missing number
  • Find the largest 2 numbers in an array

有什么方法可以使用 async - 等待可以带来绩效收益吗?如果是这样,如果只有1个处理器怎么办?那么,您的机器不是只是在任务之间分配时间,而不是同时完成任务吗?

Is there any way to do those using async-await with performance benefits? And if so, what if you only have 1 processor? Then doesn't your machine just divide its time between tasks rather than really doing them at the same time?

推荐答案

这次采访中,埃里克·利珀特(Eric Lippert)将异步等待与做早餐的厨师进行了比较。它对理解异步等待的好处大有帮助。在中间某处搜索异步等待。

In this interview, Eric Lippert compared async await with a cook making breakfast. It helped me a lot to understand the benefits of async-await. Search somewhere in the middle for 'async-await'

假设厨师必须做早餐。他必须烤些面包,煮一些鸡蛋,或者还可以煮些茶?

Suppose a cook has to make breakfast. He has to toast some bread and boil some eggs, maybe make some tea as well?

方法1:同步。由一个线程执行。您开始烤面包。等到面包烤完为止。取出面包。开始沸腾水,等到水烧开并插入鸡蛋。等到鸡蛋准备好并取出鸡蛋。开始用开水冲茶。等到水烧开煮茶。

Method 1: Synchronous. Performed by one thread. You start toasting the bread. Wait until the bread is toasted. Remove the bread. Start boiling water, wait until the water boils and insert your egg. Wait until the egg is ready and remove the egg. Start boiling water for the tea. Wait until the water is boiled and make the tea.

您会看到所有等待的地方。

Youl see all the waits. while the thread is waiting it could do other things.

方法2:异步等待,仍然是一个线程。您开始烘烤面包。在烤面包时,您开始为鸡蛋和茶开水。然后,您开始等待。当三个任务中的任何一个完成时,您将执行任务的第二部分,具体取决于首先完成的任务。因此,如果鸡蛋的水先沸腾了,您就煮鸡蛋,然后再次等待任何任务完成。

Method 2: Async-await, still one thread You start toasting the bread. While the bread is being toasted you start boiling water for the eggs and also for the tea. Then you start waiting. When any of the three tasks is finished you do the second part of the task, depending on which task finished first. So if the water for the eggs boils first, you cook the eggs, and again wait for any of the tasks to finish.

在此描述中,只有一个人(您)正在做所有的事情。仅涉及一个线程。令人高兴的是,因为只有一个线程在做这些事情,所以代码对读者来说看起来很同步,并且不需要太多使变量成为线程安全。

In this description only one person (you) is doing all the stuff. Only one thread is involved. The nice thing is, that because there is only one thread doing the stuff the code looks quite synchronous to the reader and there is not much need to make your variables thread safe.

很容易看到,这样您的早餐将在更短的时间内准备好(并且您的面包仍然很热!)。在计算机生活中,当您的线程必须等待另一个进程完成(例如,将文件写入磁盘,从数据库或Internet获取信息)时,就会发生这些事情。这些通常是在函数中看到该函数的异步版本的函数:写入 WriteAsync Read ReadAsync

It's easy to see that this way your breakfast will be ready in shorter time (and your bread will still be warm!). In computer life these things will happen when your thread has to wait for another process to finish, like writing a file to a disk, getting information from a database or from the internet. Those are typically the kind of functions where'll see an async version of the function: Write and WriteAsync, Read and ReadAsync.


另外:经过其他用户的评论和测试,我发现实际上可以是任何线程在等待后继续您的工作。此其他线程具有相同的上下文,因此可以像原始线程一样工作。

Addition: after some remarks from other users elsewhere, and some testing, I found that in fact it can be any thread who continues your work after the await. This other thread has the same 'context', and thus can act as if it was the original thread.

方法3 :在煮茶的同时雇厨师做面包烤和煮鸡蛋:真正的异步。多个线程,这是最昂贵的选择,因为它涉及创建单独的线程。在做早餐的示例中,这可能不会大大加快该过程,因为在该过程中相对较长的时间您什么也没做。但是,例如,如果您还需要切片西红柿,那么当您使用async-await进行其他操作时,让厨师(独立线程)执行此操作可能很方便。当然,等待中的一个是等待厨师完成切片。

Method 3: Hire cooks to toast the bread and boil the eggs while you make the tea: Real asynchronous. Several threads This is the most expensive option, because it involves creating separate threads. In the example of making breakfast, this will probably not speed up the process very much, because relatively large times of the process you are not doing anything anyway. But if for instance you also need to slice tomatoes, it might be handy to let a cook (separate thread) do this while you do the other stuff using async-await. Of course, one of the awaits you do is await for the cook to finish his slicing.

另一篇说明很多的文章是异步和等待由非常有帮助的斯蒂芬·克莱里(Stephen Cleary)编写。

Another article that explains a lot, is Async and Await written by the ever so helpful Stephen Cleary.

这篇关于使用async-await可以给您带来任何性能上的好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆