-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Optimize NullMath utilities (#1272)
- Loading branch information
1 parent
554b640
commit 531ae8c
Showing
5 changed files
with
232 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,72 @@ | ||
namespace Utilities; | ||
|
||
#pragma warning disable CA1805 // Do not initialize unnecessarily | ||
|
||
[TestClass] | ||
public class NullMathTests : TestBase | ||
{ | ||
private readonly double? dblPos = 100.12345; | ||
private readonly double? dblNeg = -200.98765; | ||
private readonly double? dblNul = null; | ||
private readonly decimal? decPos = 10.12345m; | ||
private readonly decimal? decNeg = -20.98765m; | ||
private readonly decimal? decNul = null; | ||
|
||
[TestMethod] | ||
public void AbsDouble() | ||
{ | ||
dblPos.Abs().Should().Be(100.12345d); | ||
dblNeg.Abs().Should().Be(200.98765d); | ||
dblNul.Abs().Should().BeNull(); | ||
} | ||
|
||
[TestMethod] | ||
public void RoundDecimal() | ||
{ | ||
decPos.Round(2).Should().Be(10.12m); | ||
decNeg.Round(2).Should().Be(-20.99m); | ||
decNul.Round(2).Should().BeNull(); | ||
|
||
10.12345m.Round(2).Should().Be(10.12m); | ||
} | ||
|
||
[TestMethod] | ||
public void RoundDouble() | ||
{ | ||
dblPos.Round(2).Should().Be(100.12d); | ||
dblNeg.Round(2).Should().Be(-200.99d); | ||
dblNul.Round(2).Should().BeNull(); | ||
|
||
100.12345d.Round(2).Should().Be(100.12d); | ||
} | ||
|
||
[TestMethod] | ||
public void Null2NaN() | ||
{ | ||
// doubles | ||
dblPos.Null2NaN().Should().Be(100.12345d); | ||
dblNeg.Null2NaN().Should().Be(-200.98765d); | ||
dblNul.Null2NaN().Should().Be(double.NaN); | ||
|
||
// decimals » doubles | ||
decPos.Null2NaN().Should().Be(10.12345d); | ||
decNeg.Null2NaN().Should().Be(-20.98765d); | ||
decNul.Null2NaN().Should().Be(double.NaN); | ||
} | ||
|
||
[TestMethod] | ||
public void NaN2Null() | ||
{ | ||
// double (nullable) | ||
double? dblNaNul = double.NaN; | ||
dblNaNul.NaN2Null().Should().BeNull(); | ||
dblPos.NaN2Null().Should().Be(100.12345d); | ||
dblNeg.NaN2Null().Should().Be(-200.98765d); | ||
|
||
// double (non-nullable) | ||
double dblNaN = double.NaN; | ||
dblNaN.NaN2Null().Should().BeNull(); | ||
100.12345d.NaN2Null().Should().Be(100.12345d); | ||
(-200.98765d).NaN2Null().Should().Be(-200.98765d); | ||
} | ||
} |
This file contains 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
This file contains 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,64 @@ | ||
namespace Tests.Performance; | ||
|
||
#pragma warning disable CA1805 // Do not initialize unnecessarily | ||
|
||
[ShortRunJob] | ||
public class UtilityNullMath | ||
{ | ||
private static readonly double? dblVal = 54321.0123456789d; | ||
private static readonly double? dblNul = null; | ||
private static readonly decimal? decVal = 54321.0123456789m; | ||
private static readonly decimal? decNul = null; | ||
private static readonly double? nulNaN = double.NaN; | ||
private const double dblNaN = double.NaN; | ||
|
||
// Abs() | ||
|
||
[Benchmark] | ||
public double? AbsDblVal() => dblVal.Abs(); | ||
|
||
[Benchmark] | ||
public double? AbsDblNul() => dblNul.Abs(); | ||
|
||
// Round() | ||
|
||
[Benchmark] | ||
public decimal? RoundDecVal() => decVal.Round(2); | ||
|
||
[Benchmark] | ||
public decimal? RoundDecNul() => decNul.Round(2); | ||
|
||
[Benchmark] | ||
public double? RoundDblVal() => dblVal.Round(2); | ||
|
||
[Benchmark] | ||
public double? RoundDblNul() => dblNul.Round(2); | ||
|
||
// Null2NaN() | ||
|
||
[Benchmark] | ||
public double Null2NaNDecVal() => decVal.Null2NaN(); | ||
|
||
[Benchmark] | ||
public double Null2NaNDecNul() => decNul.Null2NaN(); | ||
|
||
[Benchmark] | ||
public double Null2NaNDblVal() => dblVal.Null2NaN(); | ||
|
||
[Benchmark] | ||
public double Null2NaNDblNul() => dblNul.Null2NaN(); | ||
|
||
// Nan2Null() | ||
|
||
[Benchmark] | ||
public double? NaN2NullDblVal() => dblVal.NaN2Null(); | ||
|
||
[Benchmark] | ||
public double? NaN2NullDblNul() => dblNul.NaN2Null(); | ||
|
||
[Benchmark] | ||
public double? NaN2NullNaNVal() => dblNaN.NaN2Null(); | ||
|
||
[Benchmark] | ||
public double? NaN2NullNanNul() => nulNaN.NaN2Null(); | ||
} |
10 changes: 5 additions & 5 deletions
10
tests/performance/Perf.Internals.cs → tests/performance/Perf.Utility.StdDev.cs
This file contains 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 |
---|---|---|
@@ -1,22 +1,22 @@ | ||
namespace Tests.Performance; | ||
|
||
// INTERNAL FUNCTIONS | ||
// INTERNAL UTILITIES | ||
|
||
[ShortRunJob] | ||
public class InternalsPerformance | ||
public class UtilityStdDev | ||
{ | ||
[Params(20, 50, 250, 1000)] | ||
public int Periods; | ||
|
||
private double[] values; | ||
private double[] _values; | ||
|
||
// standard deviation | ||
[GlobalSetup(Targets = [nameof(StdDev)])] | ||
public void Setup() | ||
=> values = TestData.GetLongish(Periods) | ||
=> _values = TestData.GetLongish(Periods) | ||
.Select(x => (double)x.Close) | ||
.ToArray(); | ||
|
||
[Benchmark] | ||
public object StdDev() => values.StdDev(); | ||
public object StdDev() => _values.StdDev(); | ||
} |