Skip to content

QuantityInfo: internalizing the UnitInfo construction #1555

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

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here's the before (still using Enum.GetValues(typeof(MassUnit)).Cast<MassUnit>().ToArray() on both targets):

Method Job Runtime NbIterations Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
CreateDefaultMass .NET 9.0 .NET 9.0 1000 804.5 μs 16.03 μs 27.22 μs 1.00 0.05 351.5625 6.8359 5.62 MB 1.00
CreateDefaultVolume .NET 9.0 .NET 9.0 1000 1,384.4 μs 21.80 μs 18.21 μs 1.72 0.06 494.1406 13.6719 7.9 MB 1.41
CreateDefaultVolumeFlow .NET 9.0 .NET 9.0 1000 1,938.2 μs 36.43 μs 35.78 μs 2.41 0.09 634.7656 25.3906 10.15 MB 1.81
CreateDefaultMass .NET Framework 4.8 .NET Framework 4.8 1000 7,751.9 μs 59.94 μs 56.07 μs 1.00 0.01 1250.0000 15.6250 7.56 MB 1.00
CreateDefaultVolume .NET Framework 4.8 .NET Framework 4.8 1000 14,789.7 μs 85.69 μs 80.16 μs 1.91 0.02 1937.5000 46.8750 11.71 MB 1.55
CreateDefaultVolumeFlow .NET Framework 4.8 .NET Framework 4.8 1000 20,617.8 μs 94.10 μs 88.02 μs 2.66 0.02 2562.5000 93.7500 15.42 MB 2.04

After:

Method Job Runtime NbIterations Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
CreateDefaultMass .NET 9.0 .NET 9.0 1000 2.351 ms 0.0185 ms 0.0173 ms 1.00 0.01 429.6875 7.8125 6.86 MB 1.00
CreateDefaultVolume .NET 9.0 .NET 9.0 1000 3.175 ms 0.0333 ms 0.0311 ms 1.35 0.02 609.3750 - 9.86 MB 1.44
CreateDefaultVolumeFlow .NET 9.0 .NET 9.0 1000 3.750 ms 0.0368 ms 0.0326 ms 1.60 0.02 789.0625 11.7188 12.63 MB 1.84
CreateDefaultMass .NET Framework 4.8 .NET Framework 4.8 1000 2.578 ms 0.0153 ms 0.0143 ms 1.00 0.01 1265.6250 27.3438 7.6 MB 1.00
CreateDefaultVolume .NET Framework 4.8 .NET Framework 4.8 1000 3.652 ms 0.0270 ms 0.0253 ms 1.42 0.01 2035.1563 62.5000 12.24 MB 1.61
CreateDefaultVolumeFlow .NET Framework 4.8 .NET Framework 4.8 1000 4.486 ms 0.0216 ms 0.0202 ms 1.74 0.01 2367.1875 78.1250 14.21 MB 1.87

This is quite remarkable really, just for the fact that for the first time we've got a benchmark in which .NET Framework 4.8 is neck-a-neck with .NET 9.0 🤣

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;

namespace UnitsNet.Benchmark.Initializations;

[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net48)]
[SimpleJob(RuntimeMoniker.Net90)]
public class QuantityInfoInitializationBenchmarks
{
[Params(1000)]
public int NbIterations { get; set; }

[Benchmark(Baseline = true)]
public void CreateDefaultMass()
{
for (var i = 0; i < NbIterations; i++)
{
var quantityInfo = Mass.MassInfo.CreateDefault();
}
}

[Benchmark]
public void CreateDefaultVolume()
{
for (var i = 0; i < NbIterations; i++)
{
var quantityInfo = Volume.VolumeInfo.CreateDefault();
}
}

[Benchmark]
public void CreateDefaultVolumeFlow()
{
for (var i = 0; i < NbIterations; i++)
{
var quantityInfo = VolumeFlow.VolumeFlowInfo.CreateDefault();
}
}
}