-
Notifications
You must be signed in to change notification settings - Fork 397
Enum boxing optimizations (UnitKey) #1507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aca82fe
Enum boxing optimizations:
lipchev 03973bf
- QuantityInfoLookup: replaced the ICollection<QuantityInfo> paramete…
lipchev 6b4d05a
- added explicit Equals / HashCode implementations for the UnitKey (u…
lipchev 19c211a
adding the (now missing) deconstructor test
lipchev 8e2f897
QuantityInfoLookup: removed the (still) unused _quantitiesByType dic…
lipchev 6dc5f09
- QuantityInfoLookup: introduced an array of QuantityInfo and replace…
lipchev 9456bec
Merge branch 'release/v6' into fix-unit-conversions
lipchev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
UnitsNet.Benchmark/Conversions/FromString/QuantityFromStringBenchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
using UnitsNet.Units; | ||
|
||
namespace UnitsNet.Benchmark.Conversions.FromString; | ||
|
||
[MemoryDiagnoser] | ||
[SimpleJob(RuntimeMoniker.Net48)] | ||
[SimpleJob(RuntimeMoniker.Net80)] | ||
public class QuantityFromStringBenchmarks | ||
{ | ||
private static readonly CultureInfo Culture = CultureInfo.InvariantCulture; | ||
private static readonly string ValueToParse = 123.456.ToString(Culture); | ||
|
||
private readonly Random _random = new(42); | ||
private string[] _quantitiesToParse; | ||
|
||
[Params(1000)] | ||
public int NbAbbreviations { get; set; } | ||
|
||
[GlobalSetup(Target = nameof(FromMassString))] | ||
public void PrepareMassStrings() | ||
{ | ||
// can't have "mg" or "g" (see Acceleration.StandardGravity) and who knows what more... | ||
_quantitiesToParse = _random.GetItems(["kg", "lbs", "Mlbs"], NbAbbreviations).Select(abbreviation => $"{ValueToParse} {abbreviation}").ToArray(); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromVolumeUnitAbbreviation))] | ||
public void PrepareVolumeStrings() | ||
{ | ||
_quantitiesToParse = _random.GetItems(["ml", "l", "cm³", "m³"], NbAbbreviations).Select(abbreviation => $"{ValueToParse} {abbreviation}").ToArray();; | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromPressureUnitAbbreviation))] | ||
public void PreparePressureUnits() | ||
{ | ||
_quantitiesToParse = _random.GetRandomAbbreviations<PressureUnit>(UnitsNetSetup.Default.UnitAbbreviations, NbAbbreviations).Select(abbreviation => $"{ValueToParse} {abbreviation}").ToArray();; | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromVolumeFlowUnitAbbreviation))] | ||
public void PrepareVolumeFlowUnits() | ||
{ | ||
// can't have "bpm" (see Frequency) | ||
_quantitiesToParse = | ||
_random.GetItems( | ||
UnitsNetSetup.Default.UnitAbbreviations.GetAllUnitAbbreviationsForQuantity(typeof(VolumeFlowUnit)).Where(x => x != "bpm").ToArray(), | ||
NbAbbreviations).Select(abbreviation => $"{ValueToParse} {abbreviation}").ToArray(); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public IQuantity FromMassString() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var quantityString in _quantitiesToParse) | ||
{ | ||
quantity = Quantity.Parse(Culture, typeof(Mass), quantityString); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromVolumeUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var quantityString in _quantitiesToParse) | ||
{ | ||
quantity = Quantity.Parse(Culture, typeof(Volume), quantityString); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromPressureUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var quantityString in _quantitiesToParse) | ||
{ | ||
quantity = Quantity.Parse(Culture, typeof(Pressure), quantityString); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromVolumeFlowUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var quantityString in _quantitiesToParse) | ||
{ | ||
quantity = Quantity.Parse(Culture, typeof(VolumeFlow), quantityString); | ||
} | ||
|
||
return quantity; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
UnitsNet.Benchmark/Conversions/FromString/QuantityFromUnitAbbreviationBenchmarks.cs
lipchev marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
using UnitsNet.Units; | ||
|
||
namespace UnitsNet.Benchmark.Conversions.FromString; | ||
|
||
[MemoryDiagnoser] | ||
[SimpleJob(RuntimeMoniker.Net48)] | ||
[SimpleJob(RuntimeMoniker.Net80)] | ||
public class QuantityFromUnitAbbreviationBenchmarks | ||
{ | ||
private static readonly CultureInfo Culture = CultureInfo.InvariantCulture; | ||
private readonly Random _random = new(42); | ||
private string[] _massUnits; | ||
private string[] _pressureUnits; | ||
private string[] _volumeFlowUnits; | ||
private string[] _volumeUnits = []; | ||
|
||
[Params(1000)] | ||
public int NbAbbreviations { get; set; } | ||
|
||
[GlobalSetup(Target = nameof(FromMassUnitAbbreviation))] | ||
public void PrepareMassUnits() | ||
{ | ||
// can't have "mg" or "g" (see Acceleration.StandardGravity) and who knows what more... | ||
_massUnits = _random.GetItems(["kg", "lbs", "Mlbs"], NbAbbreviations); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromVolumeUnitAbbreviation))] | ||
public void PrepareVolumeUnits() | ||
{ | ||
_volumeUnits = _random.GetItems(["ml", "l", "cm³", "m³"], NbAbbreviations); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromPressureUnitAbbreviation))] | ||
public void PreparePressureUnits() | ||
{ | ||
_pressureUnits = _random.GetRandomAbbreviations<PressureUnit>(UnitsNetSetup.Default.UnitAbbreviations, NbAbbreviations); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromVolumeFlowUnitAbbreviation))] | ||
public void PrepareVolumeFlowUnits() | ||
{ | ||
// can't have "bpm" (see Frequency) | ||
_volumeFlowUnits = | ||
_random.GetItems( | ||
UnitsNetSetup.Default.UnitAbbreviations.GetAllUnitAbbreviationsForQuantity(typeof(VolumeFlowUnit)).Where(x => x != "bpm").ToArray(), | ||
NbAbbreviations); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public IQuantity FromMassUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitToParse in _massUnits) | ||
{ | ||
quantity = Quantity.FromUnitAbbreviation(Culture, 1, unitToParse); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromVolumeUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitToParse in _volumeUnits) | ||
{ | ||
quantity = Quantity.FromUnitAbbreviation(Culture, 1, unitToParse); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromPressureUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitToParse in _pressureUnits) | ||
{ | ||
quantity = Quantity.FromUnitAbbreviation(Culture, 1, unitToParse); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromVolumeFlowUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitToParse in _volumeFlowUnits) | ||
{ | ||
quantity = Quantity.FromUnitAbbreviation(Culture, 1, unitToParse); | ||
} | ||
|
||
return quantity; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
UnitsNet.Benchmark/Conversions/FromString/QuantityFromUnitNameBenchmarks.cs
lipchev marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
|
||
namespace UnitsNet.Benchmark.Conversions.FromString; | ||
|
||
[MemoryDiagnoser] | ||
[SimpleJob(RuntimeMoniker.Net48)] | ||
[SimpleJob(RuntimeMoniker.Net80)] | ||
public class QuantityFromUnitNameBenchmarks | ||
{ | ||
private readonly Random _random = new(42); | ||
private string[] _unitNames; | ||
|
||
[Params(1000)] | ||
public int NbAbbreviations { get; set; } | ||
|
||
[GlobalSetup(Target = nameof(FromMassUnitName))] | ||
public void PrepareMassUnits() | ||
{ | ||
_unitNames = _random.GetItems(Mass.Info.UnitInfos.Select(x => x.Name).ToArray(), NbAbbreviations); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromVolumeUnitName))] | ||
public void PrepareVolumeUnits() | ||
{ | ||
_unitNames = _random.GetItems(Volume.Info.UnitInfos.Select(x => x.Name).ToArray(), NbAbbreviations); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromPressureUnitName))] | ||
public void PreparePressureUnits() | ||
{ | ||
_unitNames = _random.GetItems(Pressure.Info.UnitInfos.Select(x => x.Name).ToArray(), NbAbbreviations); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromVolumeFlowUnitName))] | ||
public void PrepareVolumeFlowUnits() | ||
{ | ||
_unitNames = _random.GetItems(VolumeFlow.Info.UnitInfos.Select(x => x.Name).ToArray(), NbAbbreviations); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public IQuantity FromMassUnitName() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitName in _unitNames) | ||
{ | ||
quantity = Quantity.From(1, nameof(Mass), unitName); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromVolumeUnitName() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitName in _unitNames) | ||
{ | ||
quantity = Quantity.From(1, nameof(Volume), unitName); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromPressureUnitName() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitName in _unitNames) | ||
{ | ||
quantity = Quantity.From(1, nameof(Pressure), unitName); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromVolumeFlowUnitName() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitName in _unitNames) | ||
{ | ||
quantity = Quantity.From(1, nameof(VolumeFlow), unitName); | ||
} | ||
|
||
return quantity; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
UnitsNet.Benchmark/Conversions/ToString/ToStringWithDefaultPrecisionBenchmarks.cs
lipchev marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
using UnitsNet.Units; | ||
|
||
namespace UnitsNet.Benchmark.Conversions.ToString; | ||
|
||
[MemoryDiagnoser] | ||
[SimpleJob(RuntimeMoniker.Net48)] | ||
[SimpleJob(RuntimeMoniker.Net80)] | ||
public class ToStringWithDefaultPrecisionBenchmarks | ||
{ | ||
private static readonly double Value = 123.456; | ||
private readonly Random _random = new(42); | ||
|
||
private Mass[] _masses = []; | ||
private VolumeFlow[] _volumeFlows = []; | ||
|
||
[Params(1000)] | ||
public int NbConversions { get; set; } | ||
|
||
[Params("G", "S", "E", "N", "A")] | ||
public string Format { get; set; } | ||
|
||
[GlobalSetup(Target = nameof(MassToString))] | ||
public void PrepareMassesToTest() | ||
{ | ||
_masses = _random.GetRandomQuantities<Mass, MassUnit>(Value, Mass.Units, NbConversions).ToArray(); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(VolumeFlowToString))] | ||
public void PrepareVolumeFlowsToTest() | ||
{ | ||
_volumeFlows = _random.GetRandomQuantities<VolumeFlow, VolumeFlowUnit>(Value, VolumeFlow.Units, NbConversions).ToArray(); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void MassToString() | ||
{ | ||
foreach (Mass quantity in _masses) | ||
{ | ||
var result = quantity.ToString(Format, CultureInfo.InvariantCulture); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void VolumeFlowToString() | ||
{ | ||
foreach (VolumeFlow quantity in _volumeFlows) | ||
{ | ||
var result = quantity.ToString(Format, CultureInfo.InvariantCulture); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.