Skip to content

Instantly share code, notes, and snippets.

@aensidhe
Last active April 4, 2020 08:02
Show Gist options
  • Save aensidhe/b4fc28978e2553e68b7a7d6ea0e8ef10 to your computer and use it in GitHub Desktop.
Save aensidhe/b4fc28978e2553e68b7a7d6ea0e8ef10 to your computer and use it in GitHub Desktop.
Compiler still can't write if for me. M3 variant - all credits to @Ilchert
BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=3.1.100
  [Host]     : .NET Core 3.1.0 (CoreCLR 4.700.19.56402, CoreFX 4.700.19.56404), X64 RyuJIT
  Job-TJOYHS : .NET Core 2.2.8 (CoreCLR 4.6.28207.03, CoreFX 4.6.28208.02), X64 RyuJIT
  Job-OVZXZY : .NET Core 3.1.0 (CoreCLR 4.700.19.56402, CoreFX 4.700.19.56404), X64 RyuJIT

Method Runtime Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
Simple .NET Core 2.2 36.663 ns 0.5377 ns 0.5029 ns 1.00 0.00 0.0152 - - 72 B
OldStyle .NET Core 2.2 31.899 ns 0.2006 ns 0.1876 ns 0.87 0.01 0.0152 - - 72 B
AsTask .NET Core 2.2 42.833 ns 0.7668 ns 0.6403 ns 1.17 0.02 0.0305 - - 144 B
AsLocal .NET Core 2.2 39.972 ns 0.6643 ns 0.6214 ns 1.09 0.02 0.0152 - - 72 B
M3 .NET Core 2.2 16.196 ns 0.1952 ns 0.1630 ns 0.44 0.01 0.0152 - - 72 B
Simple .NET Core 3.1 36.609 ns 0.8249 ns 1.1830 ns 1.00 0.00 0.0153 - - 72 B
OldStyle .NET Core 3.1 27.483 ns 0.2813 ns 0.2493 ns 0.75 0.03 0.0153 - - 72 B
AsTask .NET Core 3.1 30.320 ns 0.4179 ns 0.3909 ns 0.83 0.04 0.0306 - - 144 B
AsLocal .NET Core 3.1 38.721 ns 0.3611 ns 0.3201 ns 1.06 0.03 0.0153 - - 72 B
M3 .NET Core 3.1 9.113 ns 0.1769 ns 0.1568 ns 0.25 0.01 0.0153 - - 72 B
using System;
using System.IO;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
namespace TaskBench
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<Benchmark>();
}
}
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.NetCoreApp22)]
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
public class Benchmark
{
private ValueTask<int> F() => new ValueTask<int>(42);
[Benchmark(Baseline = true)]
public async Task<int> Simple() => await F();
[Benchmark]
public async Task<int> OldStyle()
{
var f = F();
return f.IsCompletedSuccessfully ? f.Result : await f;
}
[Benchmark]
public async Task<int> AsTask() => await F().AsTask();
[Benchmark]
public Task<int> AsLocal()
{
return Do().AsTask();
async ValueTask<int> Do() => await F();
}
[Benchmark]
public Task<int> M3()
{
var vt = F();
if(vt.IsCompletedSuccessfully)
return Task.FromResult(vt.Result);
return Do(vt).AsTask();
async ValueTask<int> Do(ValueTask<int> valueTask)
=> await valueTask;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment