Skip to content

Commit

Permalink
benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
pimbrouwers committed Dec 7, 2024
1 parent 332125e commit 488559c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Danom.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Danom.Mvc.Tests", "test\Dan
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestHelpers", "test\TestHelpers\TestHelpers.csproj", "{B78AD031-6E72-4006-A007-ABA49570F17E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DanomBenchmark", "benchmark\DanomBenchmark.csproj", "{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -58,6 +60,10 @@ Global
{B78AD031-6E72-4006-A007-ABA49570F17E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B78AD031-6E72-4006-A007-ABA49570F17E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B78AD031-6E72-4006-A007-ABA49570F17E}.Release|Any CPU.Build.0 = Release|Any CPU
{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2E1327A1-9244-49BA-BC31-9CCE3CE2335C} = {606C9E49-7696-48B1-A799-B456E680C9F0}
Expand Down
17 changes: 17 additions & 0 deletions benchmark/DanomBenchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.*" />
<PackageReference Include="CSharpFunctionalExtensions" Version="3.*" />
<PackageReference Include="OneOf" Version="3.*" />
<ProjectReference Include="..\src\Danom\Danom.csproj" />
</ItemGroup>

</Project>
59 changes: 59 additions & 0 deletions benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using CSharpFunctionalExtensions;
using Danom;
using OneOf;
using OneOf.Types;

#pragma warning disable CA1822 // Mark members as static

static class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<MemoryBenchmark>();
}
}

[MemoryDiagnoser]
public class MemoryBenchmark
{
private const int Iterations = 100;

[Benchmark(Baseline = true)]
public void Nullable()
{
for (var i = 0; i < Iterations; i++)
{
var x = new int?(i);
var _ = x is int y ? y % 2 : 0;
}
}

[Benchmark]
public void CSharpFunctionalExtensions()
{
for (var i = 0; i < Iterations; i++)
{
var _ = Maybe<int>.From(i).Match(i => i % 2, () => 0);
}
}

[Benchmark]
public void Danom()
{
for (var i = 0; i < Iterations; i++)
{
var _ = Option<int>.Some(i).Match(i => i % 2, () => 0);
}
}

[Benchmark]
public void OneOf()
{
for (var i = 0; i < Iterations; i++)
{
var _ = OneOf<int, None>.FromT0(i).Match(i => i % 2, _ => 0);
}
}
}

0 comments on commit 488559c

Please sign in to comment.