Replies: 1 comment 3 replies
-
You should use BenchmarkDotNet. Context switching is expensive. Whether that's switching to a separate thread or posting to the task scheduler. You gave it the option |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've attempted to time the overhead of a simple async state machine method, vs a similar non-async
ContinueWith
-based method, full source.On a tight loop with 10,000,000 iterations, the difference is nearly non-existent with single-threaded asynchrony (a thread with
@stephentoub's
AsyncPump
-like event loop); that's expected.Doing the same with multi-threaded asynchrony (where
Task.Run
is used for scheduling on thread pool) shows ~1-2s difference, even with a fixed number of pool threads and forced garbage collection. It's still a marginal difference, but I wonder why switching threads is more costly withasync
here. Or whether I have flaws in the timing logic.To give it a bit more context, here's the Twiter thread that prompted me to do this test.
Edited, I've increased the number of iterations to 25,000,000 and the number of worker pool threads to 2000, and I now see nearly the same async overhead across single-threaded and multi-threaded tests, which is what I'd expect.
Beta Was this translation helpful? Give feedback.
All reactions