Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does async and await increase performance of an ASP.Net application

I recently read an article about c#-5 and new & nice asynchronous programming features . I see it works greate in windows application. The question came to me is if this feature can increase ASP.Net performance?

consider this two psudo code:

public T GetData()
{
    var d = GetSomeData();
    return d;
}

and

public async T GetData2()
{
    var d = await GetSomeData();   
    return d;
}

Has in an ASP.Net appication that two codes difference?

thanks

like image 400
Arian Avatar asked Mar 26 '12 05:03

Arian


People also ask

Does async await increase performance?

The main benefits of asynchronous programming using async / await include the following: Increase the performance and responsiveness of your application, particularly when you have long-running operations that do not require to block the execution.

Does async affect performance?

Asynchronicity and PerformanceTaking a synchronous operation and making it asynchronous will invariably degrade the performance of that one operation, as it still needs to accomplish everything that the synchronous operation did, but now with additional constraints and considerations.

Why async and await is used in asp net core?

When we don't want to return a result from our async method, we should always return a Task. To validate our asynchronous operations, we have to use the await keyword while calling that operation. When we convert our synchronous code to asynchronous, we have to use the async and await keywords all the way up the chain.


1 Answers

Well for a start your second piece of code would return Task<T> rather than T. The ultimate answer is "it depends".

If your page needs to access multiple data sources, it may make it simpler to parallelize access to those sources, using the result of each access only where necessary. So for example, you may want to start making a long-running data fetch as the first part of your page handling, then only need the result at the end. It's obviously possible to do this without using async/await, but it's a lot simpler when the language is helping you out.

Additionally, asynchrony can be used to handle a huge number of long-running requests on a small number of threads, if most of those requests will be idle for a lot of the time - for example in a long-polling scenario. I can see the async abilities being used as an alternative to SignalR in some scenarios.

The benefits of async on the server side are harder to pin down than on the client side because there are different ways in which it helps - whereas the "avoid doing work on the UI thread" side is so obvious, it's easy to see the benefit.

Don't forget that there can be a lot more to server-side coding than just the front-end. In my experience, async is most likely to be useful when implementing RPC services, particularly those which talk to multiple other RPC services.

As Pasi says, it's just syntactic sugar - but I believe that it's sufficiently sweet sugar that it may well make the difference between proper asynchronous handling being feasible and it being simply too much effort and complexity.

like image 87
Jon Skeet Avatar answered Sep 22 '22 16:09

Jon Skeet