(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 :)
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 ?
C# Language Async-Await Async/await will only improve performance if it allows the machine to do additional work.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With