diff --git a/src/NetFabric.Numerics.Benchmarks/AdditionBenchmarks.cs b/src/NetFabric.Numerics.Benchmarks/AdditionBenchmarks.cs new file mode 100644 index 0000000..783af9f --- /dev/null +++ b/src/NetFabric.Numerics.Benchmarks/AdditionBenchmarks.cs @@ -0,0 +1,117 @@ +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Configs; +using System.Numerics; + +namespace NetFabric.Numerics.Benchmarks; + +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] +[CategoriesColumn] +public class AdditionBenchmarks +{ + Vector2[]? vector2; + + Rectangular2D.Vector[]? rectangular2_int; + Rectangular2D.Vector[]? rectangular2_long; + Rectangular2D.Vector[]? rectangular2_float; + Rectangular2D.Vector[]? rectangular2_double; + + Polar.Vector[]? polar_float; + Polar.Vector[]? polar_double; + + [Params(10_000)] + public int Count { get; set; } + + [GlobalSetup] + public void GlobalSetup() + { + vector2 = GetEnumerable(Count).Select<(int x, int y), Vector2>(item => new(item.x, item.y)).ToArray(); + + rectangular2_int = GetEnumerable(Count).Select<(int x, int y), Rectangular2D.Vector>(item => new(item.x, item.y)).ToArray(); + rectangular2_long = GetEnumerable(Count).Select<(int x, int y), Rectangular2D.Vector>(item => new(item.x, item.y)).ToArray(); + rectangular2_float = GetEnumerable(Count).Select<(int x, int y), Rectangular2D.Vector>(item => new(item.x, item.y)).ToArray(); + rectangular2_double = GetEnumerable(Count).Select<(int x, int y), Rectangular2D.Vector>(item => new(item.x, item.y)).ToArray(); + + polar_float = GetEnumerable(Count).Select<(int x, int y), Polar.Vector>(item => new(item.x, new(item.y))).ToArray(); + polar_double = GetEnumerable(Count).Select<(int x, int y), Polar.Vector>(item => new(item.x, new(item.y))).ToArray(); + + static IEnumerable<(int x, int y)> GetEnumerable(int count) + { + var random = new Random(42); + for (var index = 0; index < count; index++) + { + yield return (random.Next(count), random.Next(count)); + } + } + } + + [BenchmarkCategory("Int")] + [Benchmark(Baseline = true)] + public Rectangular2D.Vector Rectangular2D_Int() + { + var sum = Rectangular2D.Vector.Zero; + foreach (var item in rectangular2_int!) + sum += item; + return sum; + } + + [BenchmarkCategory("Long")] + [Benchmark(Baseline = true)] + public Rectangular2D.Vector Rectangular2D_Long() + { + var sum = Rectangular2D.Vector.Zero; + foreach (var item in rectangular2_long!) + sum += item; + return sum; + } + + [BenchmarkCategory("Float")] + [Benchmark(Baseline = true)] + public Vector2 Vector2() + { + var sum = System.Numerics.Vector2.Zero; + foreach (var item in vector2!) + sum += item; + return sum; + } + + [BenchmarkCategory("Float")] + [Benchmark] + public Rectangular2D.Vector Rectangular2D_Float() + { + var sum = Rectangular2D.Vector.Zero; + foreach (var item in rectangular2_float!) + sum += item; + return sum; + } + + [BenchmarkCategory("Float")] + [Benchmark] + public Polar.Vector Polar_Float() + { + var sum = Polar.Vector.Zero; + foreach (var item in polar_float!) + sum += item; + return sum; + } + + [BenchmarkCategory("Double")] + [Benchmark(Baseline = true)] + public Rectangular2D.Vector Rectangular2D_Double() + { + var sum = Rectangular2D.Vector.Zero; + foreach (var item in rectangular2_double!) + sum += item; + return sum; + } + + [BenchmarkCategory("Double")] + [Benchmark] + public Polar.Vector Polar_Double() + { + var sum = Polar.Vector.Zero; + foreach (var item in polar_double!) + sum += item; + return sum; + } + +} \ No newline at end of file diff --git a/src/NetFabric.Numerics.Benchmarks/AdditionVectorDoubleBenchmark.cs b/src/NetFabric.Numerics.Benchmarks/AdditionVectorDoubleBenchmark.cs index 8743e90..5ce12f3 100644 --- a/src/NetFabric.Numerics.Benchmarks/AdditionVectorDoubleBenchmark.cs +++ b/src/NetFabric.Numerics.Benchmarks/AdditionVectorDoubleBenchmark.cs @@ -2,7 +2,7 @@ using BenchmarkDotNet.Configs; using NetFabric.Numerics; -namespace Benchmarks; +namespace NetFabric.Numerics.Benchmarks; [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] [CategoriesColumn] diff --git a/src/NetFabric.Numerics.Benchmarks/NetFabric.Numerics.Benchmarks.csproj b/src/NetFabric.Numerics.Benchmarks/NetFabric.Numerics.Benchmarks.csproj index e781fb1..4027b47 100644 --- a/src/NetFabric.Numerics.Benchmarks/NetFabric.Numerics.Benchmarks.csproj +++ b/src/NetFabric.Numerics.Benchmarks/NetFabric.Numerics.Benchmarks.csproj @@ -20,7 +20,6 @@ - diff --git a/src/NetFabric.Numerics.Benchmarks/Program.cs b/src/NetFabric.Numerics.Benchmarks/Program.cs index f7ef8da..8f2f86a 100644 --- a/src/NetFabric.Numerics.Benchmarks/Program.cs +++ b/src/NetFabric.Numerics.Benchmarks/Program.cs @@ -25,8 +25,8 @@ .WithSummaryStyle(SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend)) .HideColumns(Column.EnvironmentVariables, Column.RatioSD, Column.Error) .AddDiagnoser(MemoryDiagnoser.Default) - .AddDiagnoser(new DisassemblyDiagnoser(new DisassemblyDiagnoserConfig - (exportGithubMarkdown: true, printInstructionAddresses: false))) + // .AddDiagnoser(new DisassemblyDiagnoser(new DisassemblyDiagnoserConfig + // (exportGithubMarkdown: true, printInstructionAddresses: false))) .AddExporter(MarkdownExporter.GitHub) .AddJob(net70.WithEnvironmentVariable("DOTNET_EnableHWIntrinsic", "0").WithId(".NET 7 Scalar").AsBaseline()) .AddJob(net80.WithEnvironmentVariable("DOTNET_EnableHWIntrinsic", "0").WithId(".NET 8 Scalar"));