Skip to content

Commit

Permalink
Use UnitConverter to do conversion functions and allow basic extensib…
Browse files Browse the repository at this point in the history
…ility (#1023)
  • Loading branch information
tmilnthorp authored Feb 8, 2022
1 parent 8d2eeb6 commit e5aba5b
Show file tree
Hide file tree
Showing 231 changed files with 16,305 additions and 15,603 deletions.
133 changes: 68 additions & 65 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ private void GenerateStaticConstructor()
Writer.WL($@"
}},
BaseUnit, Zero, BaseDimensions, QuantityType.{_quantity.Name});
RegisterDefaultConversions(DefaultConversionFunctions);
}}
");
}
Expand Down Expand Up @@ -213,9 +215,9 @@ private void GenerateInstanceConstructors()
_value = Guard.EnsureValidNumber(value, nameof(value));"
: @"
_value = value;");
Writer.WL(@"
Writer.WL($@"
_unit = firstUnitInfo?.Value ?? throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
}
}}
");
}

Expand All @@ -224,6 +226,11 @@ private void GenerateStaticProperties()
Writer.WL($@"
#region Static Properties
/// <summary>
/// The <see cref=""UnitConverter"" /> containing the default generated conversion functions for <see cref=""{_quantity.Name}"" /> instances.
/// </summary>
public static UnitConverter DefaultConversionFunctions {{ get; }} = new UnitConverter();
/// <inheritdoc cref=""IQuantity.QuantityInfo""/>
public static QuantityInfo<{_unitEnumName}> Info {{ get; }}
Expand Down Expand Up @@ -360,27 +367,29 @@ internal static void RegisterDefaultConversions(UnitConverter unitConverter)
if(unit.SingularName == _quantity.BaseUnit)
continue;

Writer.WL( $@"
unitConverter.SetConversionFunction<{_quantity.Name}>({_unitEnumName}.{_quantity.BaseUnit}, {_quantity.Name}Unit.{unit.SingularName}, quantity => quantity.ToUnit({_quantity.Name}Unit.{unit.SingularName}));");
var func = unit.FromBaseToUnitFunc.Replace("{x}", "quantity.Value");
Writer.WL($@"
unitConverter.SetConversionFunction<{_quantity.Name}>({_unitEnumName}.{_quantity.BaseUnit}, {_quantity.Name}Unit.{unit.SingularName}, quantity => new {_quantity.Name}({func}, {_quantity.Name}Unit.{unit.SingularName}));");
}

Writer.WL( $@"
Writer.WL($@"
// Register in unit converter: BaseUnit <-> BaseUnit
unitConverter.SetConversionFunction<{_quantity.Name}>({_unitEnumName}.{_quantity.BaseUnit}, {_unitEnumName}.{_quantity.BaseUnit}, quantity => quantity);
// Register in unit converter: {_quantity.Name}Unit -> BaseUnit" );
// Register in unit converter: {_quantity.Name}Unit -> BaseUnit");

foreach(var unit in _quantity.Units)
{
if(unit.SingularName == _quantity.BaseUnit)
continue;

Writer.WL($@"
unitConverter.SetConversionFunction<{_quantity.Name}>({_quantity.Name}Unit.{unit.SingularName}, {_unitEnumName}.{_quantity.BaseUnit}, quantity => quantity.ToBaseUnit());" );
var func = unit.FromUnitToBaseFunc.Replace("{x}", "quantity.Value");
Writer.WL($@"
unitConverter.SetConversionFunction<{_quantity.Name}>({_quantity.Name}Unit.{unit.SingularName}, {_unitEnumName}.{_quantity.BaseUnit}, quantity => new {_quantity.Name}({func}, {_unitEnumName}.{_quantity.BaseUnit}));");
}

Writer.WL( $@"
Writer.WL($@"
}}
/// <summary>
Expand Down Expand Up @@ -903,11 +912,42 @@ double IQuantity.As(Enum unit)
/// <summary>
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation <paramref name=""unit"" />.
/// </summary>
/// <param name=""unit"">The unit to convert to.</param>
/// <returns>A {_quantity.Name} with the specified unit.</returns>
public {_quantity.Name} ToUnit({_unitEnumName} unit)
{{
var convertedValue = GetValueAs(unit);
return new {_quantity.Name}(convertedValue, unit);
return ToUnit(unit, DefaultConversionFunctions);
}}
/// <summary>
/// Converts this {_quantity.Name} to another {_quantity.Name} using the given <paramref name=""unitConverter""/> with the unit representation <paramref name=""unit"" />.
/// </summary>
/// <param name=""unit"">The unit to convert to.</param>
/// <param name=""unitConverter"">The <see cref=""UnitConverter""/> to use for the conversion.</param>
/// <returns>A {_quantity.Name} with the specified unit.</returns>
public {_quantity.Name} ToUnit({_unitEnumName} unit, UnitConverter unitConverter)
{{
if(Unit == unit)
{{
// Already in requested units.
return this;
}}
else if(unitConverter.TryGetConversionFunction((typeof({_quantity.Name}), Unit, typeof({_quantity.Name}), unit), out var conversionFunction))
{{
// Direct conversion to requested unit found. Return the converted quantity.
var converted = conversionFunction(this);
return ({_quantity.Name})converted;
}}
else if(Unit != BaseUnit)
{{
// Direct conversion to requested unit NOT found. Convert to BaseUnit, and then from BaseUnit to requested unit.
var inBaseUnits = ToUnit(BaseUnit);
return inBaseUnits.ToUnit(unit);
}}
else
{{
throw new NotImplementedException($""Can not convert {{Unit}} to {{unit}}."");
}}
}}
/// <inheritdoc />
Expand All @@ -916,7 +956,16 @@ IQuantity IQuantity.ToUnit(Enum unit)
if(!(unit is {_unitEnumName} unitAs{_unitEnumName}))
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
return ToUnit(unitAs{_unitEnumName});
return ToUnit(unitAs{_unitEnumName}, DefaultConversionFunctions);
}}
/// <inheritdoc />
IQuantity IQuantity.ToUnit(Enum unit, UnitConverter unitConverter)
{{
if(!(unit is {_unitEnumName} unitAs{_unitEnumName}))
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
return ToUnit(unitAs{_unitEnumName}, unitConverter);
}}
/// <inheritdoc cref=""IQuantity.ToUnit(UnitSystem)""/>
Expand All @@ -941,62 +990,16 @@ IQuantity IQuantity.ToUnit(Enum unit)
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit({_unitEnumName} unit) => ToUnit(unit);
/// <inheritdoc />
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit({_unitEnumName} unit, UnitConverter unitConverter) => ToUnit(unit, unitConverter);
/// <summary>
/// Converts the current value + unit to the base unit.
/// This is typically the first step in converting from one unit to another.
/// </summary>
/// <returns>The value in the base unit representation.</returns>
private {_valueType} GetValueInBaseUnit()
{{
switch(Unit)
{{");
foreach (var unit in _quantity.Units)
{
var func = unit.FromUnitToBaseFunc.Replace("{x}", "_value");
Writer.WL($@"
case {_unitEnumName}.{unit.SingularName}: return {func};");
}

Writer.WL($@"
default:
throw new NotImplementedException($""Can not convert {{Unit}} to base units."");
}}
}}
/// <summary>
/// Converts the current value + unit to the base unit.
/// This is typically the first step in converting from one unit to another.
/// </summary>
/// <returns>The value in the base unit representation.</returns>
internal {_quantity.Name} ToBaseUnit()
{{
var baseUnitValue = GetValueInBaseUnit();
return new {_quantity.Name}(baseUnitValue, BaseUnit);
}}
/// <inheritdoc />
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);
private {_valueType} GetValueAs({_unitEnumName} unit)
{{
if(Unit == unit)
return _value;
var baseUnitValue = GetValueInBaseUnit();
switch(unit)
{{");
foreach (var unit in _quantity.Units)
{
var func = unit.FromBaseToUnitFunc.Replace("{x}", "baseUnitValue");
Writer.WL($@"
case {_unitEnumName}.{unit.SingularName}: return {func};");
}

Writer.WL(@"
default:
throw new NotImplementedException($""Can not convert {Unit} to {unit}."");
}
}
var converted = ToUnit(unit);
return ({_valueType})converted.Value;
}}
#endregion
");
Expand Down Expand Up @@ -1084,7 +1087,7 @@ public string ToString(string format, IFormatProvider? provider)
}}
#endregion
" );
");
}

private void GenerateIConvertibleMethods()
Expand Down
92 changes: 63 additions & 29 deletions CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public override string Generate()
Writer.WL(GeneratedFileHeader);
Writer.WL($@"
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
Expand All @@ -95,16 +96,37 @@ namespace UnitsNet.Tests
// ReSharper disable once PartialTypeWithSinglePart
public abstract partial class {_quantity.Name}TestsBase : QuantityTestsBase
{{");
foreach (var unit in _quantity.Units) Writer.WL($@"
foreach(var unit in _quantity.Units) Writer.WL($@"
protected abstract double {unit.PluralName}InOne{_baseUnit.SingularName} {{ get; }}");

Writer.WL("");
Writer.WL($@"
// ReSharper disable VirtualMemberNeverOverriden.Global");
foreach (var unit in _quantity.Units) Writer.WL($@"
foreach(var unit in _quantity.Units) Writer.WL($@"
protected virtual double {unit.PluralName}Tolerance {{ get {{ return 1e-5; }} }}"); Writer.WL($@"
// ReSharper restore VirtualMemberNeverOverriden.Global
protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor({_unitEnumName} unit)
{{
return unit switch
{{");
foreach(var unit in _quantity.Units) Writer.WL($@"
{GetUnitFullName(unit)} => ({unit.PluralName}InOne{_baseUnit.SingularName}, {unit.PluralName}Tolerance),");
Writer.WL($@"
_ => throw new NotSupportedException()
}};
}}
public static IEnumerable<object[]> UnitTypes = new List<object[]>
{{");
foreach(var unit in _quantity.Units)
{
Writer.WL($@"
new object[] {{ {GetUnitFullName(unit)} }},");
}
Writer.WL($@"
}};
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{{
Expand All @@ -116,14 +138,14 @@ public void DefaultCtor_ReturnsQuantityWithZeroValueAndBaseUnit()
{{
var quantity = new {_quantity.Name}();
Assert.Equal(0, quantity.Value);");
if (_quantity.BaseType == "decimal") Writer.WL($@"
if(_quantity.BaseType == "decimal") Writer.WL($@"
Assert.Equal(0m, ((IDecimalQuantity)quantity).Value);");
Writer.WL($@"
Assert.Equal({_baseUnitFullName}, quantity.Unit);
}}
");
if (_quantity.BaseType == "double") Writer.WL($@"
if(_quantity.BaseType == "double") Writer.WL($@"
[Fact]
public void Ctor_WithInfinityValue_ThrowsArgumentException()
{{
Expand Down Expand Up @@ -183,7 +205,7 @@ public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
{{
{_quantity.Name} {baseUnitVariableName} = {_quantity.Name}.From{_baseUnit.PluralName}(1);");

foreach (var unit in _quantity.Units) Writer.WL($@"
foreach(var unit in _quantity.Units) Writer.WL($@"
AssertEx.EqualTolerance({unit.PluralName}InOne{_baseUnit.SingularName}, {baseUnitVariableName}.{unit.PluralName}, {unit.PluralName}Tolerance);");
Writer.WL($@"
}}
Expand All @@ -192,7 +214,7 @@ public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit()
{{");
int i = 0;
foreach (var unit in _quantity.Units)
foreach(var unit in _quantity.Units)
{
var quantityVariable = $"quantity{i++:D2}";
Writer.WL($@"
Expand All @@ -205,7 +227,7 @@ public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit()
Writer.WL($@"
}}
");
if (_quantity.BaseType == "double") Writer.WL($@"
if(_quantity.BaseType == "double") Writer.WL($@"
[Fact]
public void From{_baseUnit.PluralName}_WithInfinityValue_ThrowsArgumentException()
{{
Expand All @@ -224,7 +246,7 @@ public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit()
public void As()
{{
var {baseUnitVariableName} = {_quantity.Name}.From{_baseUnit.PluralName}(1);");
foreach (var unit in _quantity.Units) Writer.WL($@"
foreach(var unit in _quantity.Units) Writer.WL($@"
AssertEx.EqualTolerance({unit.PluralName}InOne{_baseUnit.SingularName}, {baseUnitVariableName}.As({GetUnitFullName(unit)}), {unit.PluralName}Tolerance);");
Writer.WL($@"
}}
Expand All @@ -246,29 +268,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}}
}}
[Fact]
public void ToUnit()
[Theory]
[MemberData(nameof(UnitTypes))]
public void ToUnit({_unitEnumName} unit)
{{
var {baseUnitVariableName} = {_quantity.Name}.From{_baseUnit.PluralName}(1);");
foreach (var unit in _quantity.Units)
{
var asQuantityVariableName = $"{unit.SingularName.ToLowerInvariant()}Quantity";
var inBaseUnits = {_quantity.Name}.From(1.0, {_quantity.Name}.BaseUnit);
var converted = inBaseUnits.ToUnit(unit);
Writer.WL("");
Writer.WL($@"
var {asQuantityVariableName} = {baseUnitVariableName}.ToUnit({GetUnitFullName(unit)});
AssertEx.EqualTolerance({unit.PluralName}InOne{_baseUnit.SingularName}, (double){asQuantityVariableName}.Value, {unit.PluralName}Tolerance);
Assert.Equal({GetUnitFullName(unit)}, {asQuantityVariableName}.Unit);");
}
Writer.WL($@"
var conversionFactor = GetConversionFactor(unit);
AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
Assert.Equal(unit, converted.Unit);
}}
[Fact]
public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
[Theory]
[MemberData(nameof(UnitTypes))]
public void ToUnit_WithSameUnits_AreEqual({_unitEnumName} unit)
{{
var quantityInBaseUnit = {_quantity.Name}.From{_baseUnit.PluralName}(1).ToBaseUnit();
Assert.Equal({_quantity.Name}.BaseUnit, quantityInBaseUnit.Unit);");
Writer.WL($@"
var quantity = {_quantity.Name}.From(3.0, unit);
var toUnitWithSameUnit = quantity.ToUnit(unit);
Assert.Equal(quantity, toUnitWithSameUnit);
}}
[Theory]
[MemberData(nameof(UnitTypes))]
public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit({_unitEnumName} unit)
{{
// See if there is a unit available that is not the base unit.
var fromUnit = {_quantity.Name}.Units.FirstOrDefault(u => u != {_quantity.Name}.BaseUnit && u != {_unitEnumName}.Undefined);
// If there is only one unit for the quantity, we must use the base unit.
if(fromUnit == {_unitEnumName}.Undefined)
fromUnit = {_quantity.Name}.BaseUnit;
var quantity = {_quantity.Name}.From(3.0, fromUnit);
var converted = quantity.ToUnit(unit);
Assert.Equal(converted.Unit, unit);
}}
[Fact]
Expand Down Expand Up @@ -701,9 +735,9 @@ public void GetHashCode_Equals()
}}
");

if( _quantity.GenerateArithmetic )
if(_quantity.GenerateArithmetic)
{
Writer.WL( $@"
Writer.WL($@"
[Theory]
[InlineData(1.0)]
[InlineData(-1.0)]
Expand All @@ -716,7 +750,7 @@ public void NegationOperator_ReturnsQuantity_WithNegatedValue(double value)

Writer.WL($@"
}}
}}" );
}}");
return Writer.ToString();
}
}
Expand Down
Loading

6 comments on commit e5aba5b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'UnitsNet Benchmarks (netcoreapp50)'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: e5aba5b Previous: e341c81 Ratio
UnitsNet.Benchmark.UnitsNetBenchmarks.ToProperty 159.2675245780961 ns (± 0.5433470807691423) 8.308972553079963 ns (± 0.30834234285384077) 19.17
UnitsNet.Benchmark.UnitsNetBenchmarks.As 157.67732568478706 ns (± 0.9105617038717335) 8.681671332357332 ns (± 0.3381877246725936) 18.16
UnitsNet.Benchmark.UnitsNetBenchmarks.ToUnit 149.6124216813756 ns (± 0.675176064938718) 15.374941458615002 ns (± 0.5456507337390197) 9.73
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_As 160.9926981226055 ns (± 0.2536573782335105) 19.302405701107926 ns (± 0.5439909019734129) 8.34
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_ToUnit 169.39405896531517 ns (± 0.9097505369040623) 29.74920538182548 ns (± 1.1395401311539952) 5.69

This comment was automatically generated by workflow using github-action-benchmark.

CC: @lipchev

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnitsNet Benchmarks (netcoreapp50)

Benchmark suite Current: e5aba5b Previous: e341c81 Ratio
UnitsNet.Benchmark.UnitsNetBenchmarks.Constructor 9.52822853704421 ns (± 0.025090253899023952) 8.988075464026887 ns (± 0.32620679579334166) 1.06
UnitsNet.Benchmark.UnitsNetBenchmarks.Constructor_SI 322.7295429950886 ns (± 1.029040341661038) 308.24256696406604 ns (± 8.000322244264588) 1.05
UnitsNet.Benchmark.UnitsNetBenchmarks.FromMethod 24.457248058454166 ns (± 0.04644239541111441) 23.210392242530787 ns (± 0.5960096416770105) 1.05
UnitsNet.Benchmark.UnitsNetBenchmarks.ToProperty 159.2675245780961 ns (± 0.5433470807691423) 8.308972553079963 ns (± 0.30834234285384077) 19.17
UnitsNet.Benchmark.UnitsNetBenchmarks.As 157.67732568478706 ns (± 0.9105617038717335) 8.681671332357332 ns (± 0.3381877246725936) 18.16
UnitsNet.Benchmark.UnitsNetBenchmarks.As_SI 327.095648582764 ns (± 1.438014189649949) 310.51527858928165 ns (± 9.103546491198989) 1.05
UnitsNet.Benchmark.UnitsNetBenchmarks.ToUnit 149.6124216813756 ns (± 0.675176064938718) 15.374941458615002 ns (± 0.5456507337390197) 9.73
UnitsNet.Benchmark.UnitsNetBenchmarks.ToUnit_SI 329.6921629877179 ns (± 1.1678354257311108) 313.8778900879904 ns (± 5.689354262475625) 1.05
UnitsNet.Benchmark.UnitsNetBenchmarks.ToStringTest 1312.6490581522623 ns (± 8.723494052786021) 1253.4163810014077 ns (± 25.55352205901862) 1.05
UnitsNet.Benchmark.UnitsNetBenchmarks.Parse 44273.702553275 ns (± 244.5976415021368) 42531.44894483974 ns (± 1199.867832233879) 1.04
UnitsNet.Benchmark.UnitsNetBenchmarks.TryParseValid 44846.12356551908 ns (± 140.2718908630166) 42972.28339932449 ns (± 1231.9308610424093) 1.04
UnitsNet.Benchmark.UnitsNetBenchmarks.TryParseInvalid 48127.831561237304 ns (± 253.220969831624) 45173.97727001977 ns (± 1543.4434116238435) 1.07
UnitsNet.Benchmark.UnitsNetBenchmarks.QuantityFrom 71.71828730824943 ns (± 0.22773300532381832) 3150 ns (± 87.83100656536799) 0.02276771025658712
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_As 160.9926981226055 ns (± 0.2536573782335105) 19.302405701107926 ns (± 0.5439909019734129) 8.34
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_As_SI 325.1316638512627 ns (± 0.9018420067181974) 313.17210281257525 ns (± 8.602007501988172) 1.04
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_ToUnit 169.39405896531517 ns (± 0.9097505369040623) 29.74920538182548 ns (± 1.1395401311539952) 5.69
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_ToStringTest 1334.4304067267803 ns (± 6.965447949048769) 1229.8039179489347 ns (± 38.248967174338155) 1.09

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnitsNet Benchmarks (netcoreapp21)

Benchmark suite Current: e5aba5b Previous: e341c81 Ratio
UnitsNet.Benchmark.UnitsNetBenchmarks.Constructor 11.891646489879545 ns (± 0.41109499940680594) 13.683423722171701 ns (± 0.294628868048763) 0.87
UnitsNet.Benchmark.UnitsNetBenchmarks.Constructor_SI 553.3455271515359 ns (± 11.81895208480539) 662.95355482051 ns (± 13.598736522725414) 0.83
UnitsNet.Benchmark.UnitsNetBenchmarks.FromMethod 30.797189170488156 ns (± 0.61405490746595) 31.53873893205566 ns (± 0.4034917870893946) 0.98
UnitsNet.Benchmark.UnitsNetBenchmarks.ToProperty 215.05686795887917 ns (± 3.625485952406118) 10.94906321178836 ns (± 0.22740646133969988) 19.64
UnitsNet.Benchmark.UnitsNetBenchmarks.As 232.77466766276405 ns (± 4.625326315228878) 10.981953257733903 ns (± 0.3694240572546368) 21.20
UnitsNet.Benchmark.UnitsNetBenchmarks.As_SI 574.7856753369464 ns (± 24.759573995297323) 688.3371468256553 ns (± 17.23176322016595) 0.84
UnitsNet.Benchmark.UnitsNetBenchmarks.ToUnit 208.52813863878535 ns (± 4.746806451908159) 22.012920069270148 ns (± 0.38190370946440494) 9.47
UnitsNet.Benchmark.UnitsNetBenchmarks.ToUnit_SI 575.8790216730469 ns (± 18.584023580195005) 658.5169967841999 ns (± 9.273505941484775) 0.87
UnitsNet.Benchmark.UnitsNetBenchmarks.ToStringTest 2422.3496937701234 ns (± 41.59745043262032) 2605.9156185409474 ns (± 49.042063845974404) 0.93
UnitsNet.Benchmark.UnitsNetBenchmarks.Parse 72062.93513787231 ns (± 1248.9376332715894) 77325.48704069441 ns (± 2239.8972634902984) 0.93
UnitsNet.Benchmark.UnitsNetBenchmarks.TryParseValid 72693.69761366732 ns (± 1313.253707525675) 78333.77678253813 ns (± 2429.447445100901) 0.93
UnitsNet.Benchmark.UnitsNetBenchmarks.TryParseInvalid 79314.5923149016 ns (± 1187.4072217760747) 84855.34032925774 ns (± 1740.1696868597987) 0.93
UnitsNet.Benchmark.UnitsNetBenchmarks.QuantityFrom 110.4504992793666 ns (± 1.3848827853535577) 2691.780821917808 ns (± 133.07623614715308) 0.0410325010045484
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_As 229.65238332842108 ns (± 3.6590406665831186) 24.726702135830823 ns (± 0.5811742873432862) 9.29
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_As_SI 547.0623966489782 ns (± 9.72606688824364) 644.294389419918 ns (± 8.8460166778799) 0.85
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_ToUnit 230.13582809209203 ns (± 6.281576543006521) 35.296309937212804 ns (± 0.6171507982647606) 6.52
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_ToStringTest 2472.8509313465706 ns (± 32.61434720391898) 2654.517314708867 ns (± 30.73583092650037) 0.93

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'UnitsNet Benchmarks (netcoreapp21)'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: e5aba5b Previous: e341c81 Ratio
UnitsNet.Benchmark.UnitsNetBenchmarks.ToProperty 215.05686795887917 ns (± 3.625485952406118) 10.94906321178836 ns (± 0.22740646133969988) 19.64
UnitsNet.Benchmark.UnitsNetBenchmarks.As 232.77466766276405 ns (± 4.625326315228878) 10.981953257733903 ns (± 0.3694240572546368) 21.20
UnitsNet.Benchmark.UnitsNetBenchmarks.ToUnit 208.52813863878535 ns (± 4.746806451908159) 22.012920069270148 ns (± 0.38190370946440494) 9.47
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_As 229.65238332842108 ns (± 3.6590406665831186) 24.726702135830823 ns (± 0.5811742873432862) 9.29
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_ToUnit 230.13582809209203 ns (± 6.281576543006521) 35.296309937212804 ns (± 0.6171507982647606) 6.52

This comment was automatically generated by workflow using github-action-benchmark.

CC: @lipchev

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'UnitsNet Benchmarks (net472)'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: e5aba5b Previous: e341c81 Ratio
UnitsNet.Benchmark.UnitsNetBenchmarks.ToProperty 293.18471495027876 ns (± 5.4840107027259375) 7.926924497708099 ns (± 0.42045207673733054) 36.99
UnitsNet.Benchmark.UnitsNetBenchmarks.As 298.13086221493074 ns (± 6.8929372322534155) 7.535495820621575 ns (± 0.06423681294476093) 39.56
UnitsNet.Benchmark.UnitsNetBenchmarks.ToUnit 293.85010029805846 ns (± 8.918249864021876) 17.672890278827662 ns (± 0.5384998312434098) 16.63
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_As 306.6060298002434 ns (± 4.5183851268595765) 17.715057952494934 ns (± 0.54330157537113) 17.31
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_ToUnit 315.92363340210903 ns (± 6.765978833259235) 26.38794379846414 ns (± 0.5618953807909987) 11.97

This comment was automatically generated by workflow using github-action-benchmark.

CC: @lipchev

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnitsNet Benchmarks (net472)

Benchmark suite Current: e5aba5b Previous: e341c81 Ratio
UnitsNet.Benchmark.UnitsNetBenchmarks.Constructor 15.5968610293695 ns (± 0.23127685737856338) 11.720710407161912 ns (± 0.5513988197226473) 1.33
UnitsNet.Benchmark.UnitsNetBenchmarks.Constructor_SI 607.9319158469277 ns (± 10.92707218331886) 456.6619523738062 ns (± 11.549141545324357) 1.33
UnitsNet.Benchmark.UnitsNetBenchmarks.FromMethod 36.38055006589663 ns (± 0.6292359385142119) 27.52885628607952 ns (± 1.420088490037617) 1.32
UnitsNet.Benchmark.UnitsNetBenchmarks.ToProperty 293.18471495027876 ns (± 5.4840107027259375) 7.926924497708099 ns (± 0.42045207673733054) 36.99
UnitsNet.Benchmark.UnitsNetBenchmarks.As 298.13086221493074 ns (± 6.8929372322534155) 7.535495820621575 ns (± 0.06423681294476093) 39.56
UnitsNet.Benchmark.UnitsNetBenchmarks.As_SI 618.7046880494022 ns (± 13.275291033196652) 449.92911582958175 ns (± 7.440376694304617) 1.38
UnitsNet.Benchmark.UnitsNetBenchmarks.ToUnit 293.85010029805846 ns (± 8.918249864021876) 17.672890278827662 ns (± 0.5384998312434098) 16.63
UnitsNet.Benchmark.UnitsNetBenchmarks.ToUnit_SI 646.0313680658295 ns (± 12.77688161009029) 487.72823496056986 ns (± 22.926550095050818) 1.32
UnitsNet.Benchmark.UnitsNetBenchmarks.ToStringTest 2422.3483101838833 ns (± 43.716626789513825) 1715.806233747826 ns (± 100.59307463672073) 1.41
UnitsNet.Benchmark.UnitsNetBenchmarks.Parse 68528.95133057226 ns (± 1962.2006260569053) 49033.68578899753 ns (± 1690.8497664708946) 1.40
UnitsNet.Benchmark.UnitsNetBenchmarks.TryParseValid 70240.99106417547 ns (± 1232.7794188732087) 51141.76730117251 ns (± 2405.840013505295) 1.37
UnitsNet.Benchmark.UnitsNetBenchmarks.TryParseInvalid 73492.22384784199 ns (± 932.3317755387818) 55074.923437218524 ns (± 2450.812758537697) 1.33
UnitsNet.Benchmark.UnitsNetBenchmarks.QuantityFrom 122.87521051887278 ns (± 2.050422838502803) 2065.3846153846152 ns (± 83.74708747552651) 0.05949265313762928
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_As 306.6060298002434 ns (± 4.5183851268595765) 17.715057952494934 ns (± 0.54330157537113) 17.31
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_As_SI 613.4936523758766 ns (± 11.503903783395817) 473.45150086638137 ns (± 20.111318913521124) 1.30
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_ToUnit 315.92363340210903 ns (± 6.765978833259235) 26.38794379846414 ns (± 0.5618953807909987) 11.97
UnitsNet.Benchmark.UnitsNetBenchmarks.IQuantity_ToStringTest 2362.03150020315 ns (± 33.07757153105004) 1649.4820182242966 ns (± 44.81373107769935) 1.43

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.