Created
April 24, 2024 09:39
-
-
Save mgravell/777cb9fe40879cab69015dc1d7bfaf07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using System; | |
using System.Threading.Tasks; | |
#if DEBUG | |
var obj = new AsyncMachineryBenchmarks(); | |
// we should have done 100 things (just yields) either way | |
Console.WriteLine(await obj.Recursive()); // 100 | |
Console.WriteLine(await obj.Loop()); // 100 | |
#else | |
BenchmarkRunner.Run(typeof(AsyncMachineryBenchmarks).Assembly, args: args); | |
#endif | |
[MemoryDiagnoser] | |
public class AsyncMachineryBenchmarks | |
{ | |
[Params(100)] | |
public int Count { get; set; } = 100; | |
[Benchmark(Baseline = true)] | |
public Task<int> Recursive() => RecurseAsync(100); | |
static async Task<int> RecurseAsync(int count) | |
{ | |
await Task.Yield(); | |
return count > 1 ? (1 + await RecurseAsync(count - 1)) : 1; | |
} | |
[Benchmark] | |
public async Task<int> Loop() | |
{ | |
int total = 0, count = Count; | |
while (count-- > 0) | |
{ | |
await Task.Yield(); | |
total++; | |
} | |
return total; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
note we can ignore the Mean here - that's going to be hugely limited by randomness of `Yield` and the thread-pool performance, | |
which is fast but not very deterministic; what we're interested in is the allocations, which is the machinery overheads | |
| Method | Count | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | | |
|---------- |------ |---------:|---------:|---------:|------:|--------:|-------:|----------:|------------:| | |
| Recursive | 100 | 32.96 us | 0.312 us | 0.292 us | 1.00 | 0.00 | 0.6104 | 10460 B | 1.00 | | |
| Loop | 100 | 41.63 us | 0.429 us | 0.381 us | 1.26 | 0.02 | - | 171 B | 0.02 | | |
BenchmarkDotNet v0.13.12, Windows 11 (10.0.26100.1) | |
AMD Ryzen 9 7900X, 1 CPU, 24 logical and 12 physical cores | |
.NET SDK 8.0.203 | |
[Host] : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI | |
DefaultJob : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment