Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/Atmoos.Quantities.Benchmark/AddingQuantities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Units.Imperial.Length;
using Atmoos.Quantities.Units.Si;
using BenchmarkDotNet.Diagnosers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Atmoos.Quantities.Benchmark;
namespace Atmoos.Quantities.Benchmark;

file interface IFactory<out T>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@
<ProjectReference Include="..\Atmoos.Quantities.Serialization\Text.Json\Atmoos.Quantities.Serialization.Text.Json.csproj" />
<ProjectReference Include="..\Atmoos.Quantities.Serialization\Newtonsoft\Atmoos.Quantities.Serialization.Newtonsoft.csproj" />
</ItemGroup>

</Project>
7 changes: 6 additions & 1 deletion source/Atmoos.Quantities.Benchmark/Convenience.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ namespace Atmoos.Quantities.Benchmark;
internal static class Convenience
{
private static readonly JsonSerializerOptions options = new JsonSerializerOptions().EnableQuantities(typeof(Day).Assembly);

public static String Serialize<T>(this T value) => JsonSerializer.Serialize(value, options);

public static T Deserialize<T>(this String value) => JsonSerializer.Deserialize<T>(value, options) ?? throw new Exception($"Deserialization of {typeof(T).Name} failed");
public static T Deserialize<T>(this String value, JsonSerializerSettings settings) => JsonConvert.DeserializeObject<T>(value, settings) ?? throw new Exception($"Deserialization of {typeof(T).Name} failed");

public static T Deserialize<T>(this String value, JsonSerializerSettings settings) =>
JsonConvert.DeserializeObject<T>(value, settings) ?? throw new Exception($"Deserialization of {typeof(T).Name} failed");

public static Polynomial Poly(in Double nominator = 1, in Double denominator = 1, in Double offset = 0)
{
var value = new Transformation();
Expand Down
6 changes: 4 additions & 2 deletions source/Atmoos.Quantities.Benchmark/ConversionBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using Atmoos.Quantities.Core.Numerics;

namespace Atmoos.Quantities.Benchmark;
Expand Down Expand Up @@ -45,9 +45,11 @@ private Double Trivial(Double value)
}

file static class Cache<TFrom, TTo>
where TFrom : ITransform where TTo : ITransform
where TFrom : ITransform
where TTo : ITransform
{
private static readonly Polynomial polynomial = Polynomial.Of<TFrom>() / Polynomial.Of<TTo>();

public static Double Convert(in Double value) => polynomial * value;
}

Expand Down
2 changes: 1 addition & 1 deletion source/Atmoos.Quantities.Benchmark/ConvertingQuantities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Units.Imperial.Length;
using Atmoos.Quantities.Units.Si;
using Atmoos.Quantities.Units.Si.Derived;
Expand Down
9 changes: 8 additions & 1 deletion source/Atmoos.Quantities.Benchmark/CreateQuantities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Units.Si;
using Atmoos.Quantities.Units.Si.Derived;
Expand All @@ -11,22 +11,29 @@ internal readonly struct DummyQuantity
{
private readonly Double value;
public Double Value => this.value;

public DummyQuantity(in Double value) => this.value = value;
}

public sealed class DummyObject : ICastOperators<DummyObject, Double>
{
private readonly DummyQuantity value;

private DummyObject(in DummyQuantity value) => this.value = value;

public static DummyObject Of(in Double value) => new(new DummyQuantity(in value));

public static implicit operator Double(DummyObject obj) => obj.value.Value;
}

public readonly struct DummyStruct : ICastOperators<DummyStruct, Double>
{
private readonly DummyQuantity value;

private DummyStruct(in DummyQuantity value) => this.value = value;

public static DummyStruct Of(in Double value) => new(new DummyQuantity(in value));

public static implicit operator Double(DummyStruct obj) => obj.value.Value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Units.Si;
using Atmoos.Quantities.Units.Si.Derived;
using Atmoos.Quantities.Units.Si.Metric;
Expand All @@ -18,16 +18,22 @@ public class DeserializationBenchmark

[Benchmark(Baseline = true)]
public (Double, String, String) SystemTriple() => triple.Deserialize<(Double, String, String)>();

[Benchmark]
public Length SystemQuantity() => simpleQuantity.Deserialize<Length>();

[Benchmark]
public Length PrefixedQuantity() => prefixedQuantity.Deserialize<Length>();

[Benchmark]
public Velocity FractionalQuantity() => fractionalQuantity.Deserialize<Velocity>();

[Benchmark]
public Energy MultiplicativeQuantity() => multiplicativeQuantity.Deserialize<Energy>();

[Benchmark]
public Volume PowerQuantity() => powerQuantity.Deserialize<Volume>();

[Benchmark]
public Volume ScalarPowerQuantity() => scalarPowerQuantity.Deserialize<Volume>();

Expand Down
2 changes: 1 addition & 1 deletion source/Atmoos.Quantities.Benchmark/DividingQuantities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Physics;
using Atmoos.Quantities.Physics;
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Units.Imperial.Area;
using Atmoos.Quantities.Units.Imperial.Length;
Expand Down
10 changes: 5 additions & 5 deletions source/Atmoos.Quantities.Benchmark/MeasureBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Measures;
using Atmoos.Quantities.Measures;
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Units.Imperial.Length;
using Atmoos.Quantities.Units.Si;
Expand Down Expand Up @@ -33,9 +33,9 @@ .NET SDK 10.0.100
DefaultJob : .NET 10.0.0 (10.0.0, 42.42.42.42424), X64 RyuJIT x86-64-v3


| Method | Mean | Error | Ratio |
| Method | Mean | Error | Ratio |
|----------------- |----------:|----------:|------:|
| ProjectTrivial | 0.1133 ns | 0.0332 ns | 1.14 |
| ProjectOntoSame | 4.8860 ns | 0.0146 ns | 49.02 |
| ProjectOntoOther | 4.7861 ns | 0.0072 ns | 48.02 |
| ProjectTrivial | 0.1133 ns | 0.0332 ns | 1.14 |
| ProjectOntoSame | 4.8860 ns | 0.0146 ns | 49.02 |
| ProjectOntoOther | 4.7861 ns | 0.0072 ns | 48.02 |
*/
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Physics;
using Atmoos.Quantities.Physics;
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Units.Imperial.Length;
using Atmoos.Quantities.Units.Si;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Core.Numerics;
using Atmoos.Quantities.Core.Numerics;

namespace Atmoos.Quantities.Benchmark.Numerics;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Atmoos.Quantities.Core.Numerics;

using Atmoos.Quantities.Core.Numerics;
using static Atmoos.Quantities.Benchmark.Convenience;
using static Atmoos.Quantities.Benchmark.Numerics.Trivial;

Expand All @@ -16,8 +15,10 @@ public class PolynomialBenchmark

[Benchmark(Baseline = true)]
public Double EvaluateTrivial() => Poly(in trivial, argument);

[Benchmark]
public Double EvaluatePolynomial() => polynomial * argument;

[Benchmark]
public Double EvaluatePolynomialWithoutOffset() => polynomialWithoutOffset * argument;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Atmoos.Quantities.Core.Numerics;

using Atmoos.Quantities.Core.Numerics;
using static Atmoos.Quantities.Benchmark.Convenience;
using static Atmoos.Quantities.Benchmark.Numerics.Trivial;

Expand All @@ -16,6 +15,7 @@ public class PolynomialExponentiationBenchmark

[Benchmark(Baseline = true)]
public Double TrivialExp() => Poly(PolyExp(in trivial, Exponent), argument);

[Benchmark]
public Double PolynomialExp() => polynomial.Pow(Exponent) * argument;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Atmoos.Quantities.Core.Numerics;

using Atmoos.Quantities.Core.Numerics;
using static Atmoos.Quantities.Benchmark.Convenience;
using static Atmoos.Quantities.Benchmark.Numerics.Trivial;

Expand All @@ -25,10 +24,13 @@ public void Setup()

[Benchmark(Baseline = true)]
public Double TrivialImplementation() => Poly(in this.trivialB, Poly(in this.trivialA, in this.argument));

[Benchmark]
public Double PolynomialMultiplication() => this.left * this.right * this.argument;

[Benchmark]
public Double PolynomialDivision() => this.left / this.right * this.argument;

[Benchmark]
public Double PolynomialPowerOfTwo() => this.left.Pow(2) * this.argument;

Expand All @@ -44,10 +46,10 @@ .NET SDK 10.0.100
DefaultJob : .NET 10.0.0 (10.0.0, 42.42.42.42424), X64 RyuJIT x86-64-v3


| Method | Mean | Error | Ratio |
| Method | Mean | Error | Ratio |
|------------------------- |----------:|----------:|------:|
| TrivialImplementation | 4.0531 ns | 0.1131 ns | 1.00 |
| PolynomialMultiplication | 1.2274 ns | 0.0015 ns | 0.30 |
| PolynomialDivision | 1.2449 ns | 0.0086 ns | 0.31 |
| PolynomialPowerOfTwo | 0.7591 ns | 0.0083 ns | 0.19 |
| TrivialImplementation | 4.0531 ns | 0.1131 ns | 1.00 |
| PolynomialMultiplication | 1.2274 ns | 0.0015 ns | 0.30 |
| PolynomialDivision | 1.2449 ns | 0.0086 ns | 0.31 |
| PolynomialPowerOfTwo | 0.7591 ns | 0.0083 ns | 0.19 |
*/
1 change: 0 additions & 1 deletion source/Atmoos.Quantities.Benchmark/Numerics/Trivial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Atmoos.Quantities.Benchmark.Numerics;

public static class Trivial
{

[MethodImpl(MethodImplOptions.NoInlining)]
public static Double Poly(in (Double a, Double b, Double c) poly, in Double value) => poly.a * value / poly.b + poly.c;

Expand Down
2 changes: 1 addition & 1 deletion source/Atmoos.Quantities.Benchmark/ParsingBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Parsing;
using Atmoos.Quantities.Parsing;

namespace Atmoos.Quantities.Benchmark;

Expand Down
5 changes: 3 additions & 2 deletions source/Atmoos.Quantities.Benchmark/PrefixScalingBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Prefixes;

namespace Atmoos.Quantities.Benchmark;

Expand Down Expand Up @@ -38,7 +38,8 @@ private sealed class ToDouble : IPrefixInject<Double>
{
public Double Identity(in Double value) => value;

public Double Inject<TPrefix>(in Double value) where TPrefix : IPrefix => value;
public Double Inject<TPrefix>(in Double value)
where TPrefix : IPrefix => value;
}
}

Expand Down
1 change: 0 additions & 1 deletion source/Atmoos.Quantities.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;

using static BenchmarkDotNet.Columns.StatisticColumn;

// dotnet run -c Release --project Quantities.Benchmark/
Expand Down
6 changes: 3 additions & 3 deletions source/Atmoos.Quantities.Benchmark/QuantitiesBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Units.Si;

namespace Atmoos.Quantities.Benchmark;
Expand Down Expand Up @@ -32,8 +32,8 @@ .NET SDK 10.0.100
[Host] : .NET 10.0.0 (10.0.0, 42.42.42.42424), X64 RyuJIT x86-64-v3
ShortRun : .NET 10.0.0 (10.0.0, 42.42.42.42424), X64 RyuJIT x86-64-v3

Job=ShortRun IterationCount=3 LaunchCount=1
WarmupCount=3
Job=ShortRun IterationCount=3 LaunchCount=1
WarmupCount=3

| Method | Mean | Error | Allocated |
|----------------------------- |----------:|----------:|----------:|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Prefixes;
using Atmoos.Quantities.Serialization.Newtonsoft;
using Atmoos.Quantities.Units.Si;
using Newtonsoft.Json;
Expand Down
20 changes: 18 additions & 2 deletions source/Atmoos.Quantities.Benchmark/Trivial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,53 @@ public static TOther To<TSelf, TOther>(this TSelf self, TOther other)
public readonly struct Si<TUnit> : ITrivialQuantity<Si<TUnit>>
where TUnit : ISiUnit
{
private readonly Double value, scale;
private readonly Double value,
scale;
public Prefix Prefix { get; }

private Si(in Double value, in Prefix prefix)
{
this.value = value;
this.Prefix = prefix;
this.scale = Math.Pow(10, (Int32)prefix);
}

private Si(in Double value, in Double scale, Prefix prefix)
{
this.value = value;
this.scale = scale;
this.Prefix = prefix;
}

public Double ToSi() => this.scale * this.value;

public Si<TUnit> FromSi(in Double value) => new(value / this.scale, in this.scale, this.Prefix);

public static Si<TUnit> Unit(in Double value) => new(value, Prefix.Unit);

public static Si<TUnit> Of(Prefix prefix, in Double value) => new(value, prefix);

public static implicit operator Double(Si<TUnit> trivial) => trivial.value;

public static Si<TUnit> operator +(Si<TUnit> left, Si<TUnit> right)
{
Double sum = Add(left.ToSi(), right.ToSi());
return left.FromSi(in sum);
}

public static Si<TUnit> operator -(Si<TUnit> left, Si<TUnit> right)
{
Double sum = Subtract(left.ToSi(), right.ToSi());
return left.FromSi(in sum);
}

public static Si<TUnit> operator *(Si<TUnit> left, Si<TUnit> right)
{
Prefix prefix = left.Prefix | right.Prefix;
Double product = left.ToSi() * right.ToSi() / Math.Pow(10, (Int32)prefix);
return new Si<TUnit>(in product, in prefix);
}

public static Si<TUnit> operator /(Si<TUnit> left, Si<TUnit> right)
{
Prefix prefix = left.Prefix & ~right.Prefix;
Expand All @@ -88,14 +100,18 @@ private Si(in Double value, in Double scale, Prefix prefix)
public readonly struct Imperial<TUnit> : ITrivialQuantity<Imperial<TUnit>>
where TUnit : IImperialUnit
{
private readonly Double value, scale;
private readonly Double value,
scale;

public Imperial(in Double value, in Double scaleToSi)
{
this.value = value;
this.scale = scaleToSi;
}

public Double ToSi() => this.scale * this.value;

public Imperial<TUnit> FromSi(in Double value) => new(value / this.scale, in this.scale);

public static implicit operator Double(Imperial<TUnit> self) => self.value;
}
1 change: 0 additions & 1 deletion source/Atmoos.Quantities.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
<ItemGroup Condition="$(IsPackable)">
<InternalsVisibleTo Include="$(AssemblyName).Test" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion source/Atmoos.Quantities.Pack.targets
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@
<ItemGroup>
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.39" PrivateAssets="All" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
<ProjectReference Include="..\..\Atmoos.Quantities.Units\Atmoos.Quantities.Units.csproj" />
<ProjectReference Include="..\Newtonsoft\Atmoos.Quantities.Serialization.Newtonsoft.csproj" />
</ItemGroup>

</Project>
Loading