diff --git a/Danom.sln b/Danom.sln index bcd5de3..ecf6147 100644 --- a/Danom.sln +++ b/Danom.sln @@ -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 @@ -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} diff --git a/benchmark/DanomBenchmark.csproj b/benchmark/DanomBenchmark.csproj new file mode 100644 index 0000000..63c7021 --- /dev/null +++ b/benchmark/DanomBenchmark.csproj @@ -0,0 +1,17 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + + + + diff --git a/benchmark/Program.cs b/benchmark/Program.cs new file mode 100644 index 0000000..1a63c4d --- /dev/null +++ b/benchmark/Program.cs @@ -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(); + } +} + +[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.From(i).Match(i => i % 2, () => 0); + } + } + + [Benchmark] + public void Danom() + { + for (var i = 0; i < Iterations; i++) + { + var _ = Option.Some(i).Match(i => i % 2, () => 0); + } + } + + [Benchmark] + public void OneOf() + { + for (var i = 0; i < Iterations; i++) + { + var _ = OneOf.FromT0(i).Match(i => i % 2, _ => 0); + } + } +}