Skip to content

Commit

Permalink
add performance comparison with UnitsNet.
Browse files Browse the repository at this point in the history
  • Loading branch information
atmoos committed Nov 25, 2023
1 parent 01f8d26 commit 59e447a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
33 changes: 33 additions & 0 deletions source/Quantities.Benchmark/Compare/Creation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BenchmarkDotNet.Attributes;
using Quantities.Prefixes;
using Quantities.Units.Si;

namespace Quantities.Benchmark.Compare;

[MemoryDiagnoser]
public class Creation
{
private static Double value = Math.E;

[Benchmark(Baseline = true)]
public UnitsNet.Length CreateUnitsNet() => UnitsNet.Length.FromCentimeters(value);

[Benchmark]
public Length CreateQuantity() => Length.Of(value).Si<Centi, Metre>();
}

/*
// * Summary *
BenchmarkDotNet v0.13.8, Arch Linux
Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 7.0.111
[Host] : .NET 7.0.11 (7.0.1123.46301), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.11 (7.0.1123.46301), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Ratio | Allocated | Alloc Ratio |
|--------------- |---------:|---------:|---------:|------:|----------:|------------:|
| CreateUnitsNet | 13.02 ns | 0.129 ns | 0.114 ns | 1.00 | - | NA |
| CreateQuantity | 10.70 ns | 0.046 ns | 0.038 ns | 0.82 | - | NA |
*/
39 changes: 39 additions & 0 deletions source/Quantities.Benchmark/Compare/Multiplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using BenchmarkDotNet.Attributes;
using Quantities.Prefixes;
using Quantities.Units.Imperial.Length;
using Quantities.Units.Si;

using nLength = UnitsNet.Length;

namespace Quantities.Benchmark.Compare;

[MemoryDiagnoser]
public class Multiplication
{
private static readonly Length left = Length.Of(3).Si<Milli, Metre>();
private static readonly Length right = Length.Of(4).Imperial<Foot>();
private static readonly nLength nLeft = nLength.FromMillimeters(3);
private static readonly nLength nRight = nLength.FromMillimeters(4);

[Benchmark(Baseline = true)]
public UnitsNet.Area UnitsNetMultiply() => nLeft * nRight;

[Benchmark]
public Area QuantityMultiply() => left * right;
}

/*
// * Summary *
BenchmarkDotNet v0.13.8, Arch Linux
Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 7.0.111
[Host] : .NET 7.0.11 (7.0.1123.46301), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.11 (7.0.1123.46301), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Ratio | Allocated | Alloc Ratio |
|----------------- |---------:|---------:|---------:|------:|----------:|------------:|
| UnitsNetMultiply | 78.01 ns | 0.527 ns | 0.493 ns | 1.00 | - | NA |
| QuantityMultiply | 10.64 ns | 0.046 ns | 0.041 ns | 0.14 | - | NA |
*/
1 change: 1 addition & 0 deletions source/Quantities.Benchmark/Quantities.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="UnitsNet" Version="5.34.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.8" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.8" Condition="'$(OS)' == 'Windows_NT'" />
</ItemGroup>
Expand Down

0 comments on commit 59e447a

Please sign in to comment.