When you choose to use async code, you’re probably thinking about performance. Whether that’s the responsiveness of a UI application, the throughput of a server, or enabling parallelism using actors, you need to know that the change is actually going to be worthwhile.
To think about the performance of async code, you have to look at it in comparison to the alternatives that are relevant in each situation. In this chapter, we will consider:
Situations with a long-running operation that has the potential to be executed asynchronously
Situations with no long-running operation, where there’s no opportunity to execute asynchronously
Comparisons of async code against standard code, which blocks during long-running operations
Comparisons of async code against manual asynchronous code
We’ll also discuss a few optimizations that can be useful if you find that the extra overhead of the async machinery is causing a performance problem in your application.
The machinery of async methods inevitably uses more processor cycles than the equivalent synchronous code, and the switches between threads add extra latency. It’s impossible to measure the performance overhead of an async method exactly. The performance in your application depends on what other threads are doing, cache behavior, and other unpredictable factors. There’s also a distinction between processor usage and added latency, since an asynchronous system can easily add time to ...