Skip to content

Commit

Permalink
Add system to the comparison benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
atmoos committed Nov 25, 2023
1 parent 59e447a commit 6fc3a3d
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 40 deletions.
41 changes: 41 additions & 0 deletions source/Quantities.Benchmark/Compare/ComplexCreation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using BenchmarkDotNet.Attributes;
using Quantities.Prefixes;
using Quantities.Units.Si;
using Quantities.Units.Si.Metric;

namespace Quantities.Benchmark.Compare;

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

[Benchmark(Baseline = true)]
public Volume CreateQuantity() => Volume.Of(value).Cubic.Si<Centi, Metre>();

[Benchmark]
public UnitsNet.Volume CreateUnitsNet() => UnitsNet.Volume.FromCubicCentimeters(value);

[Benchmark]
public Volume CreateQuantityCentiLitre() => Volume.Of(value).Metric<Centi, Litre>();

[Benchmark]
public UnitsNet.Volume CreateUnitsNetCentiLitre() => UnitsNet.Volume.FromCentiliters(value);
}
/*
// * 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 |
|------------------------- |---------:|---------:|---------:|------:|----------:|------------:|
| CreateQuantity | 23.63 ns | 0.247 ns | 0.231 ns | 1.00 | - | NA |
| CreateUnitsNet | 12.92 ns | 0.044 ns | 0.041 ns | 0.55 | - | NA |
| CreateQuantityCentiLitre | 27.19 ns | 0.068 ns | 0.064 ns | 1.15 | - | NA |
| CreateUnitsNetCentiLitre | 12.66 ns | 0.095 ns | 0.089 ns | 0.54 | - | NA |
*/
33 changes: 0 additions & 33 deletions source/Quantities.Benchmark/Compare/Creation.cs

This file was deleted.

40 changes: 40 additions & 0 deletions source/Quantities.Benchmark/Compare/Division.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using BenchmarkDotNet.Attributes;
using Quantities.Prefixes;
using Quantities.Units.Imperial.Length;
using Quantities.Units.Si;
using Quantities.Units.Si.Metric;
using nLength = UnitsNet.Length;
using nVolume = UnitsNet.Volume;

namespace Quantities.Benchmark.Compare;

[MemoryDiagnoser]
public class Division
{
private static readonly Volume left = Volume.Of(32).Metric<Centi, Litre>();
private static readonly Length right = Length.Of(4).Imperial<Foot>();
private static readonly nVolume nLeft = nVolume.FromCentiliters(32);
private static readonly nLength nRight = nLength.FromFeet(4);

[Benchmark(Baseline = true)]
public Area Quantity() => left / right;

[Benchmark]
public UnitsNet.Area UnitsNet() => nLeft / nRight;
}

/*
// * 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 | RatioSD | Allocated | Alloc Ratio |
|--------- |---------:|---------:|---------:|------:|--------:|----------:|------------:|
| Quantity | 12.06 ns | 0.055 ns | 0.046 ns | 1.00 | 0.00 | - | NA |
| UnitsNet | 80.38 ns | 0.243 ns | 0.216 ns | 6.66 | 0.03 | - | NA |
*/
12 changes: 6 additions & 6 deletions source/Quantities.Benchmark/Compare/Multiplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class Multiplication
private static readonly nLength nRight = nLength.FromMillimeters(4);

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

[Benchmark]
public Area QuantityMultiply() => left * right;
public UnitsNet.Area UnitsNet() => nLeft * nRight;
}

/*
Expand All @@ -32,8 +32,8 @@ .NET SDK 7.0.111
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 |
| Method | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio |
|--------- |---------:|---------:|---------:|------:|--------:|----------:|------------:|
| Quantity | 10.48 ns | 0.052 ns | 0.049 ns | 1.00 | 0.00 | - | NA |
| UnitsNet | 74.90 ns | 0.313 ns | 0.292 ns | 7.14 | 0.05 | - | NA |
*/
35 changes: 35 additions & 0 deletions source/Quantities.Benchmark/Compare/ScalarCreation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using BenchmarkDotNet.Attributes;
using Quantities.Prefixes;
using Quantities.Units.Si;

using nLength = UnitsNet.Length;

namespace Quantities.Benchmark.Compare;

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

[Benchmark(Baseline = true)]
public Length Quantity() => Length.Of(value).Si<Centi, Metre>();

[Benchmark]
public nLength UnitsNet() => nLength.FromCentimeters(value);
}

/*
// * 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 | RatioSD | Allocated | Alloc Ratio |
|--------- |---------:|---------:|---------:|------:|--------:|----------:|------------:|
| Quantity | 10.68 ns | 0.150 ns | 0.141 ns | 1.00 | 0.00 | - | NA |
| UnitsNet | 12.84 ns | 0.139 ns | 0.130 ns | 1.20 | 0.02 | - | NA |
*/
3 changes: 2 additions & 1 deletion source/Quantities.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
using Quantities.Benchmark;

// dotnet run -c Release --project Quantities.Benchmark/
BenchmarkRunner.Run<MultiplyingQuantities>();
//BenchmarkRunner.Run<MultiplyingQuantities>();
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

0 comments on commit 6fc3a3d

Please sign in to comment.