Skip to content

Commit

Permalink
Add addition benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Nov 3, 2023
1 parent 686a3c9 commit ebe81f9
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 4 deletions.
117 changes: 117 additions & 0 deletions src/NetFabric.Numerics.Benchmarks/AdditionBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -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<int>[]? rectangular2_int;
Rectangular2D.Vector<long>[]? rectangular2_long;
Rectangular2D.Vector<float>[]? rectangular2_float;
Rectangular2D.Vector<double>[]? rectangular2_double;

Polar.Vector<Degrees, float>[]? polar_float;
Polar.Vector<Degrees, double>[]? 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<int>>(item => new(item.x, item.y)).ToArray();
rectangular2_long = GetEnumerable(Count).Select<(int x, int y), Rectangular2D.Vector<long>>(item => new(item.x, item.y)).ToArray();
rectangular2_float = GetEnumerable(Count).Select<(int x, int y), Rectangular2D.Vector<float>>(item => new(item.x, item.y)).ToArray();
rectangular2_double = GetEnumerable(Count).Select<(int x, int y), Rectangular2D.Vector<double>>(item => new(item.x, item.y)).ToArray();

polar_float = GetEnumerable(Count).Select<(int x, int y), Polar.Vector<Degrees, float>>(item => new(item.x, new(item.y))).ToArray();
polar_double = GetEnumerable(Count).Select<(int x, int y), Polar.Vector<Degrees, double>>(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<int> Rectangular2D_Int()
{
var sum = Rectangular2D.Vector<int>.Zero;
foreach (var item in rectangular2_int!)
sum += item;
return sum;
}

[BenchmarkCategory("Long")]
[Benchmark(Baseline = true)]
public Rectangular2D.Vector<long> Rectangular2D_Long()
{
var sum = Rectangular2D.Vector<long>.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<float> Rectangular2D_Float()
{
var sum = Rectangular2D.Vector<float>.Zero;
foreach (var item in rectangular2_float!)
sum += item;
return sum;
}

[BenchmarkCategory("Float")]
[Benchmark]
public Polar.Vector<Degrees, float> Polar_Float()
{
var sum = Polar.Vector<Degrees, float>.Zero;
foreach (var item in polar_float!)
sum += item;
return sum;
}

[BenchmarkCategory("Double")]
[Benchmark(Baseline = true)]
public Rectangular2D.Vector<double> Rectangular2D_Double()
{
var sum = Rectangular2D.Vector<double>.Zero;
foreach (var item in rectangular2_double!)
sum += item;
return sum;
}

[BenchmarkCategory("Double")]
[Benchmark]
public Polar.Vector<Degrees, double> Polar_Double()
{
var sum = Polar.Vector<Degrees, double>.Zero;
foreach (var item in polar_double!)
sum += item;
return sum;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using BenchmarkDotNet.Configs;
using NetFabric.Numerics;

namespace Benchmarks;
namespace NetFabric.Numerics.Benchmarks;

[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[CategoriesColumn]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetFabric.Numerics.Angle\NetFabric.Numerics.Angle.csproj" />
<ProjectReference Include="..\NetFabric.Numerics\NetFabric.Numerics.csproj" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/NetFabric.Numerics.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit ebe81f9

Please sign in to comment.