Friday, December 18, 2015

The price of async signatures in .Net/C#

Some code is async in nature (e.g. I/O-bound) and you should write it async.
On the other side of the fence is CPU-bound code and there's no point in writing it async.

One day, as you design a library, you need to decide of whether you should make method's signatures async for the code that may as well as may not be async in nature.

Async comes at a price: CPU cycles & memory footprint.

So, I wrote a benchmark: https://gist.github.com/modosansreves/51391d38b59f68f1ae01

It runs a simple cycle adding numbers, just to keep CPU busy for a while. The best thing now is to read the code, it speaks for itself.

Here are results for my i5 machine.

Empty cycles: 10
Repetitions: 10000
Best result for pure  sync code: 234
Best result for sync async code: 464
Empty cycles: 100
Repetitions: 10000
Best result for pure  sync code: 1918
Best result for sync async code: 2265
Empty cycles: 1000
Repetitions: 10000
Best result for pure  sync code: 17685
Best result for sync async code: 17869

This benchmark ignores GC pressure, thou.

Verdict: it's totally ok to design interfaces around Task.

No comments:

Post a Comment