using BenchmarkDotNet.Attributes; using System.Runtime.CompilerServices; namespace Benchmarks.Console.Misc; [MemoryDiagnoser] public class AsyncVsReturnTask { [Benchmark] public async Task Await_CompletedTask() { await Task.CompletedTask; } [Benchmark] public Task Return_CompletedTask() { return Task.CompletedTask; } [Benchmark] [Arguments(0)] [Arguments(5)] [Arguments(10)] [Arguments(100)] public async Task Await_Iterations(int iterations) { return await CreateDummyTask(iterations); } [Benchmark] [Arguments(0)] [Arguments(5)] [Arguments(10)] [Arguments(100)] public Task Return_Iterations(int iterations) { return CreateDummyTask(iterations); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private Task CreateDummyTask(int iterations) { return Task.Run(() => { int dummy = 0; for (var i = 0; i < iterations; ++i) ++dummy; return dummy; }); } }