Skip to content

Instantly share code, notes, and snippets.

@joncloud
Created March 25, 2018 15:42
Show Gist options
  • Select an option

  • Save joncloud/4d72f8184fd0fc8cdab8e5419e2fa26b to your computer and use it in GitHub Desktop.

Select an option

Save joncloud/4d72f8184fd0fc8cdab8e5419e2fa26b to your computer and use it in GitHub Desktop.
LambdaDelegates
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using static Rlx.Functions;
namespace Rlx.BenchmarkTests
{
[MemoryDiagnoser]
public class MapBenchmark
{
[Benchmark]
public int MapSome()
=> Some(123).Map(x => x * x).Map(x => x + 1).UnwrapOrDefault();
[Benchmark]
public int MapNone()
=> None<int>().Map(x => x * x).Map(x => x + 1).UnwrapOrDefault();
[Benchmark]
public int MapNullInt()
{
int? Calculate() => null;
int? num = Calculate();
if (num.HasValue) num = num * num;
if (num.HasValue) num = num + 1;
return num ?? 0;
}
[Benchmark(Baseline = true)]
public int MapInt()
{
int? Calculate() => 123;
int? num = Calculate();
if (num.HasValue) num = num * num;
if (num.HasValue) num = num + 1;
return num ?? 0;
}
}
}
BenchmarkDotNet=v0.10.13, OS=ubuntu 16.04
Intel Core i5-2400 CPU 3.10GHz (Sandy Bridge), 1 CPU, 4 logical cores and 4 physical cores
.NET Core SDK=2.1.4
  [Host]     : .NET Core 2.0.5 (CoreCLR 4.6.0.0, CoreFX 4.6.26018.01), 64bit RyuJIT
  DefaultJob : .NET Core 2.0.5 (CoreCLR 4.6.0.0, CoreFX 4.6.26018.01), 64bit RyuJIT

Method Mean Error StdDev Scaled Allocated
MapSome 35.980 ns 0.0853 ns 0.0798 ns 3.08 0 B
MapNone 41.736 ns 0.1303 ns 0.1218 ns 3.57 0 B
MapNullInt 8.005 ns 0.0161 ns 0.0126 ns 0.69 0 B
MapInt 11.676 ns 0.0353 ns 0.0330 ns 1.00 0 B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment