Skip to content

Instantly share code, notes, and snippets.

@joncloud
Created May 27, 2020 16:07
Show Gist options
  • Save joncloud/16c972bf645ba8a5a5f5ffc5a0d702a4 to your computer and use it in GitHub Desktop.
Save joncloud/16c972bf645ba8a5a5f5ffc5a0d702a4 to your computer and use it in GitHub Desktop.
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.836 (1909/November2018Update/19H2)
Intel Core i7-8850H CPU 2.60GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=3.1.300-preview-015135
  [Host]     : .NET Core 3.1.4 (CoreCLR 4.700.20.20201, CoreFX 4.700.20.22101), X64 RyuJIT
  DefaultJob : .NET Core 3.1.4 (CoreCLR 4.700.20.20201, CoreFX 4.700.20.22101), X64 RyuJIT

Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
Task_IsSynchronous 23.67 ns 1.356 ns 3.803 ns 0.0153 - - 72 B
Task_IsAsynchronous 23.69 ns 0.507 ns 1.092 ns 0.0153 - - 72 B
ValueTask_IsSynchronous 24.78 ns 0.500 ns 0.685 ns - - - -
ValueTask_IsAsynchronous 27.25 ns 0.559 ns 0.644 ns - - - -
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Threading.Tasks;
namespace ConsoleApp92
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<Tests>();
}
}
[MemoryDiagnoser]
public class Tests
{
static bool AlwaysFalse() => false;
[Benchmark]
public async Task<int> Task_IsSynchronous()
{
if (AlwaysFalse())
{
await Task.Delay(0);
}
return 123;
}
[Benchmark]
public async Task<int> Task_IsAsynchronous()
{
await Task.Delay(0);
return 123;
}
[Benchmark]
public async ValueTask<int> ValueTask_IsSynchronous()
{
if (AlwaysFalse())
{
await Task.Delay(0);
}
return 123;
}
[Benchmark]
public async ValueTask<int> ValueTask_IsAsynchronous()
{
await Task.Delay(0);
return 123;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment