Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async await performance?

(Just a theoretical question - for non-gui apps)

Assuming I have this code with many awaits:

public async Task<T> ConsumeAsync()     {           await A();           await b();           await c();           await d();           //..     } 

Where each task can take a very short period of time ,

Question (again , theoretical)

There could be a situation where the overall time dealing with all those "releasing back threads" and "fetching threads back" ( red & green here :)

enter image description here

Is taking more time than a single thread which could done all the work with a small amount of delay ,

I mean , I wanted to be the most productive , but instead , since all those switches back and forth - I actually lost productivity.

Can such scenario occur ?

like image 370
Royi Namir Avatar asked May 26 '14 13:05

Royi Namir


People also ask

Does async-await improve performance?

C# Language Async-Await Async/await will only improve performance if it allows the machine to do additional work.

Is async-await slower?

I found out that running async-await can be much slower in some scenarios. But if I click on the 'both' button, the 'await' version is ~3-4 times slower than the promises version.

Is async-await better than then?

Async/await and then() are very similar. The difference is that in an async function, JavaScript will pause the function execution until the promise settles. With then() , the rest of the function will continue to execute but JavaScript won't execute the . then() callback until the promise settles.

Does async make things faster?

It's not faster, it just doesn't waste time. Synchronous code stops processing when waiting for I/O. Which means that when you're reading a file you can't run any other code. Now, if you have nothing else to do while that file is being read then asynchronous code would not buy you anything much.


1 Answers

Yes, in theory. Not normally, in the real world.

In the common case, async is used for I/O-bound operations, and the overhead of thread management is undetectable in comparison to them. Most of the time, asynchronous operations either take a very long time (compared to thread management) or are already completed (e.g., a cache). Note that async has a "fast path" that kicks in if the operation is already completed, where it does not yield the thread.

For more information, see the Zen of Async and Async Performance.

like image 182
Stephen Cleary Avatar answered Sep 19 '22 13:09

Stephen Cleary