Benchmarking GetAwaiter.GetResult(), .Result, and .Wait for both Task and ValueTask
In this section, we will be writing some code to benchmark the GetAwaiter.GetResult()
, .Result
, and .Wait
methods to see which method is best for obtaining the return value for both a Task
and a ValueTask
.
At https://github.com/dotnet/BenchmarkDotNet/issues/236, the BenchmarkDotNet
maintainer called adamsitnik wrote in reply to @i3arnon:
"@i3arnon Thanks for the hint! I have measured .Result
vs .Wait
vs GetAwaiter.GetResult()
and it seems that for Tasks
the GetAwaiter.GetResult()
is also the fastest way to go. On the other hand, for ValueTask
it was much more slower so I stayed with .Result
for VT."
And so, from the code that we will be writing, we should see that .Result
should provide us with the best performance when working with a ValueTask
. And GetAwaiter.GetResult()
should give us the best performance when working with a Task
.
We will now start writing our code. Please complete...