At work we were discussing should we use in our ASP.NET Core application with 20 users synchronous fast queries (such as EF's dbContext.Table.First()
) instead of the async versions due to additional time to create another thread for the async operation.
I benchmarked both versions.
The application runs on a Windows 10 computer (i5, 16 Gb, Current timer interval: 0.999 ms). The DB is on another computer in Docker in local network.
public async Task<DailyExchangeRate> GetExchangeRateAsync(int currencyId, DateTime date)
{
return await dbContext.DailyExchangeRates
.Where(dr => dr.CurrencyId == currencyId)
.FirstOrDefaultAsync(dr => dr.Date == date.Date);
}
public DailyExchangeRate GetExchangeRate(int currencyId, DateTime date)
{
return dbContext.DailyExchangeRates
.Where(dr => dr.CurrencyId == currencyId)
.FirstOrDefault(dr => dr.Date == date.Date);
}
The results are in Stopwatch.Ticks (the last column):
Why the async version is faster?