My question relates to the overall time it takes to execute an async function compared to synchronous function.
My understanding is as follows:
-Async function gets called
-Code executes until the awaited code is reached
-Whilst awaited code is executed (eg db read), the executing thread is freed up to do another task
-Once awaited code is complete, a thread is assigned to complete this call
Assuming what I say is correct, is it the case that on a busy server the async call could and often would be slower than the synchronous call?
My reasoning is as follows:
There is an overhead to async (keeping track of everything)
As the async code has to wait for a thread to execute the remaining code, it has to wait for a thread to become available - so if all the threads are busy it would have to wait for one?
If this is true, then I assume this also means that if there was an awaited method that did not do any I/O, it would still release the executing thread and wait for the next one to become available to complete the task?
UPDATE: I know await/async does not make response times faster, but instead allows the server to be more efficient.
The main point I am trying to clarify is whether a request could take longer to complete due to the fact that there are no threads available when the async task has completed eg if the db call has completed, does it ever have to wait more time for a thread to become available? I know there are many dimensions to await and many more points to consider, but I am just trying to understand this one point in isolation.
UPDATE 2: Assume I have a well designed async system, but I have one method that I need to be as fast and lean as possible - I do not ant anything to ever get in the way of the execution time for this one call. Surely in this situation, if I make this method async and free up the thread, the call could potentially have to wait for a thread to finish its execution if it is async, whereas if it is sync the thread will always be there waiting - at the expense of the performance of the rest of the application?
asyncimproves scalability by releasing threads to handle other work instead of blocking