-
Notifications
You must be signed in to change notification settings - Fork 387
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
Enum boxing optimizations (UnitKey) #1507
base: release/v6
Are you sure you want to change the base?
Changes from 1 commit
aca82fe
03973bf
6b4d05a
19c211a
8e2f897
6dc5f09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before:
After:
|
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; | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before:
After:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...must have missed to rename some of these methods.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 03973bf |
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(FromVolumeUnitAbbreviation))] | ||
public void PrepareVolumeUnits() | ||
{ | ||
_unitNames = _random.GetItems(Volume.Info.UnitInfos.Select(x => x.Name).ToArray(), NbAbbreviations); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromPressureUnitAbbreviation))] | ||
public void PreparePressureUnits() | ||
{ | ||
_unitNames = _random.GetItems(Pressure.Info.UnitInfos.Select(x => x.Name).ToArray(), NbAbbreviations); | ||
} | ||
|
||
[GlobalSetup(Target = nameof(FromVolumeFlowUnitAbbreviation))] | ||
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 FromVolumeUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitName in _unitNames) | ||
{ | ||
quantity = Quantity.From(1, nameof(Volume), unitName); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromPressureUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitName in _unitNames) | ||
{ | ||
quantity = Quantity.From(1, nameof(Pressure), unitName); | ||
} | ||
|
||
return quantity; | ||
} | ||
|
||
[Benchmark(Baseline = false)] | ||
public IQuantity FromVolumeFlowUnitAbbreviation() | ||
{ | ||
IQuantity quantity = null; | ||
foreach (var unitName in _unitNames) | ||
{ | ||
quantity = Quantity.From(1, nameof(VolumeFlow), unitName); | ||
} | ||
|
||
return quantity; | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before:
After:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .. I fixed the method names, but didn't bother re-running the benchmark.. |
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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before:
After: