Hamid Bahmanabady Hamid Bahmanabady - 23 days ago 9
C# Question

why async single task runs faster than a normal single task?

I have a method which has just one task to do and has to wait for that task to complete:

public async Task<JsonResult> GetAllAsync()
{
var result = await this.GetAllDBAsync();
return Json(result, JsonRequestBehavior.AllowGet);
}

public async Task<List<TblSubjectSubset>> GetAllDBAsync()
{
return await model.TblSubjectSubsets.ToListAsync();
}


It is significantly faster than when I run it without async-await.
We know


The async and await keywords don't cause additional threads to be
created. Async methods don't require multithreading because an async
method doesn't run on its own thread. The method runs on the current
synchronization context and uses time on the thread only when the
method is active


According to this link :https://msdn.microsoft.com/en-us/library/hh191443.aspx#BKMK_Threads

so there is question about why the reason of being faster when we don't have another thread to handle the job.

Answer

"Asynchronous" does not mean "faster."

"Asynchronous" means "performs its operation in a way that it does not require a thread for the duration of the operation, thus allowing that thread to be used for other work."

In this case, you're testing a single request. The asynchronous request will "yield" its thread to the ASP.NET thread pool... which has no other use for it, since there are no other requests.

I fully expect asynchronous handlers to run slower than synchronous handlers. This is for a variety of reasons: there's the overhead of the async/await state machine, and extra work when the task completes to have its thread enter the request context. Besides this, the Win32 API layer is still heavily optimized for synchronous calls (expect this to change gradually over the next decade or so).

So, why use asynchronous handlers then?

For scalability reasons.

Consider an ASP.NET server that is serving more than one request - hundreds or thousands of requests instead of a single one. In that case, ASP.NET will be very grateful for the thread returned to it during its request processing. It can immediately use that thread to handle other requests. Asynchronous requests allow ASP.NET to handle more requests with fewer threads.

This is assuming your backend can scale, of course. If every request has to hit a single SQL Server, then your scalability bottleneck will probably be your database, not your web server.

But if your situation calls for it, asynchronous code can be a great boost to your web server scalability.

For more information, see my article on async ASP.NET.

Comments