使用 async-await 能给你带来任何性能优势吗? [英] Can using async-await give you any performance benefits?

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

问题描述

每当我读到 async-await 时,用例示例都是总是,其中有一个您不想要的 UI冻结.要么所有的编程书籍/教程都是相同的,要么 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-await 来提高性能?如果是这样,如果您只有 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 将异步等待与做早餐的厨师进行了比较.它对我理解 async-await 的好处有很大帮助.在中间的某个地方搜索async-await"

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?

方法一:同步.由一个线程执行.你开始烤面包.等到面包烤好.取出面包.开始烧水,等到水沸腾,然后放入鸡蛋.等到鸡蛋准备好并取出鸡蛋.开始煮茶水.等水烧开再泡茶.

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:Async-await,仍然是一个线程 您开始烤面包.烤面包时,您开始为鸡蛋和茶煮沸水.然后你开始等待.当三个任务中的任何一个完成后,您将执行任务的第二部分,具体取决于哪个任务先完成.因此,如果鸡蛋的水先沸腾,您可以煮鸡蛋,然后再次等待任何任务完成.

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 获取信息.这些通常是将看到函数的异步版本的函数类型:WriteWriteAsyncReadReadAsync.

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.

补充:经过其他地方的其他用户的一些评论,并进行了一些测试,我发现实际上可以是任何线程在await之后继续您的工作.另一个线程具有相同的上下文",因此可以像原始线程一样运行.

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.

另一篇解释很多的文章是Async and Await 由非常乐于助人的 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天全站免登陆