4 ms·
I have been grafting asynchronous support into my codebase by a) writing new code with it and b) using AsyncContext [1] to run asynchronous stuff synchronously.
by taspeotis 10y ago
I have been grafting asynchronous support into my codebase by a) writing new code with it and b) using AsyncContext [1] to run asynchronous stuff synchronously.
The performance:lines of code ratio you can get with async is quite high. Using ADO.NET you can run mutually exclusive queries in parallel for up-to a 2x speedup (depending on what the queries need to do):
using (var connection1 = ...)
using (var connection2 = ...)
{
var getSomething1Task = GetSomething1Async(connection1, ..., cancellationToken);
var getSomething2Task = GetSomething2Async(connection2, ..., cancellationToken);
await Task.WhenAll(getSomething1Task, getSomething2Task);
// now aggregate results from 1 and 2
}
And it's more or less the same lines of code you'd need for the synchronous version.
[1] http://stackoverflow.com/a/9343733/242520 http://stackoverflow.com/a/9343733/242520
- tychuz 10y agoYes, multithreading is stupidly easy nowadays. I also use Parallel.ForEach and sometimes .AsParallel() - changing "header" of foreach/for cycle is easy peasy and performance gains are instant. :)