Skip to content

Commit 488559c

Browse files
committed
benchmark
1 parent 332125e commit 488559c

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

Danom.sln

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Danom.Mvc.Tests", "test\Dan
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestHelpers", "test\TestHelpers\TestHelpers.csproj", "{B78AD031-6E72-4006-A007-ABA49570F17E}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DanomBenchmark", "benchmark\DanomBenchmark.csproj", "{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -58,6 +60,10 @@ Global
5860
{B78AD031-6E72-4006-A007-ABA49570F17E}.Debug|Any CPU.Build.0 = Debug|Any CPU
5961
{B78AD031-6E72-4006-A007-ABA49570F17E}.Release|Any CPU.ActiveCfg = Release|Any CPU
6062
{B78AD031-6E72-4006-A007-ABA49570F17E}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64+
{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
65+
{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
66+
{E198BC9B-74F8-4EF3-8404-910F1F3DCD4C}.Release|Any CPU.Build.0 = Release|Any CPU
6167
EndGlobalSection
6268
GlobalSection(NestedProjects) = preSolution
6369
{2E1327A1-9244-49BA-BC31-9CCE3CE2335C} = {606C9E49-7696-48B1-A799-B456E680C9F0}

benchmark/DanomBenchmark.csproj

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.*" />
12+
<PackageReference Include="CSharpFunctionalExtensions" Version="3.*" />
13+
<PackageReference Include="OneOf" Version="3.*" />
14+
<ProjectReference Include="..\src\Danom\Danom.csproj" />
15+
</ItemGroup>
16+
17+
</Project>

benchmark/Program.cs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Running;
3+
using CSharpFunctionalExtensions;
4+
using Danom;
5+
using OneOf;
6+
using OneOf.Types;
7+
8+
#pragma warning disable CA1822 // Mark members as static
9+
10+
static class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
BenchmarkRunner.Run<MemoryBenchmark>();
15+
}
16+
}
17+
18+
[MemoryDiagnoser]
19+
public class MemoryBenchmark
20+
{
21+
private const int Iterations = 100;
22+
23+
[Benchmark(Baseline = true)]
24+
public void Nullable()
25+
{
26+
for (var i = 0; i < Iterations; i++)
27+
{
28+
var x = new int?(i);
29+
var _ = x is int y ? y % 2 : 0;
30+
}
31+
}
32+
33+
[Benchmark]
34+
public void CSharpFunctionalExtensions()
35+
{
36+
for (var i = 0; i < Iterations; i++)
37+
{
38+
var _ = Maybe<int>.From(i).Match(i => i % 2, () => 0);
39+
}
40+
}
41+
42+
[Benchmark]
43+
public void Danom()
44+
{
45+
for (var i = 0; i < Iterations; i++)
46+
{
47+
var _ = Option<int>.Some(i).Match(i => i % 2, () => 0);
48+
}
49+
}
50+
51+
[Benchmark]
52+
public void OneOf()
53+
{
54+
for (var i = 0; i < Iterations; i++)
55+
{
56+
var _ = OneOf<int, None>.FromT0(i).Match(i => i % 2, _ => 0);
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)