This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Configs;
|
||||
|
||||
namespace Just.PreciseMath.Benchmarks;
|
||||
|
||||
/// <summary>Compares same-type scalar arithmetic on ordinary finite operands.</summary>
|
||||
[MemoryDiagnoser]
|
||||
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
|
||||
[CategoriesColumn]
|
||||
public class ArithmeticBenchmarks
|
||||
{
|
||||
private double _doubleLeft;
|
||||
private double _doubleRight;
|
||||
private decimal _decimalLeft;
|
||||
private decimal _decimalRight;
|
||||
private DoubleDouble _doubleDoubleLeft;
|
||||
private DoubleDouble _doubleDoubleRight;
|
||||
|
||||
/// <summary>One binary-exact input and one decimal input with a binary residual.</summary>
|
||||
[Params(1.25, 1.1)]
|
||||
public double Left { get; set; }
|
||||
|
||||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
// Conversion and construction are deliberately outside the timed methods.
|
||||
// Decimal and DoubleDouble represent the same decimal input; double rounds it.
|
||||
_doubleLeft = Left;
|
||||
_doubleRight = 0.75;
|
||||
_decimalLeft = (decimal)Left;
|
||||
_decimalRight = 0.75m;
|
||||
_doubleDoubleLeft = (DoubleDouble)_decimalLeft;
|
||||
_doubleDoubleRight = (DoubleDouble)_decimalRight;
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true), BenchmarkCategory("Addition")]
|
||||
public double DoubleAdd()
|
||||
{
|
||||
return _doubleLeft + _doubleRight;
|
||||
}
|
||||
|
||||
[Benchmark, BenchmarkCategory("Addition")]
|
||||
public decimal DecimalAdd()
|
||||
{
|
||||
return _decimalLeft + _decimalRight;
|
||||
}
|
||||
|
||||
[Benchmark, BenchmarkCategory("Addition")]
|
||||
public DoubleDouble DoubleDoubleAdd()
|
||||
{
|
||||
return _doubleDoubleLeft + _doubleDoubleRight;
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true), BenchmarkCategory("Subtraction")]
|
||||
public double DoubleSubtract()
|
||||
{
|
||||
return _doubleLeft - _doubleRight;
|
||||
}
|
||||
|
||||
[Benchmark, BenchmarkCategory("Subtraction")]
|
||||
public decimal DecimalSubtract()
|
||||
{
|
||||
return _decimalLeft - _decimalRight;
|
||||
}
|
||||
|
||||
[Benchmark, BenchmarkCategory("Subtraction")]
|
||||
public DoubleDouble DoubleDoubleSubtract()
|
||||
{
|
||||
return _doubleDoubleLeft - _doubleDoubleRight;
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true), BenchmarkCategory("Multiplication")]
|
||||
public double DoubleMultiply()
|
||||
{
|
||||
return _doubleLeft * _doubleRight;
|
||||
}
|
||||
|
||||
[Benchmark, BenchmarkCategory("Multiplication")]
|
||||
public decimal DecimalMultiply()
|
||||
{
|
||||
return _decimalLeft * _decimalRight;
|
||||
}
|
||||
|
||||
[Benchmark, BenchmarkCategory("Multiplication")]
|
||||
public DoubleDouble DoubleDoubleMultiply()
|
||||
{
|
||||
return _doubleDoubleLeft * _doubleDoubleRight;
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true), BenchmarkCategory("Division")]
|
||||
public double DoubleDivide()
|
||||
{
|
||||
return _doubleLeft / _doubleRight;
|
||||
}
|
||||
|
||||
[Benchmark, BenchmarkCategory("Division")]
|
||||
public decimal DecimalDivide()
|
||||
{
|
||||
return _decimalLeft / _decimalRight;
|
||||
}
|
||||
|
||||
[Benchmark, BenchmarkCategory("Division")]
|
||||
public DoubleDouble DoubleDoubleDivide()
|
||||
{
|
||||
return _doubleDoubleLeft / _doubleDoubleRight;
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,8 @@
|
||||
<PropertyGroup>
|
||||
<AssemblyName>Just.PreciseMath.Benchmarks</AssemblyName>
|
||||
<RootNamespace>Just.PreciseMath.Benchmarks</RootNamespace>
|
||||
<Description>BenchmarkDotNet scaffold for measuring Just.PreciseMath arithmetic and mathematical functions.</Description>
|
||||
<!-- Source-free scaffold: change to Exe when adding the benchmark entry point. -->
|
||||
<OutputType>Library</OutputType>
|
||||
<Description>BenchmarkDotNet comparisons of DoubleDouble, decimal, and double arithmetic.</Description>
|
||||
<OutputType>Exe</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using BenchmarkDotNet.Reports;
|
||||
using BenchmarkDotNet.Running;
|
||||
|
||||
namespace Just.PreciseMath.Benchmarks;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
BenchmarkSwitcher switcher = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly);
|
||||
// Only these standalone discovery/help forms intentionally produce no reports.
|
||||
if (args is ["--list", "flat" or "tree"] or ["--help"] or ["-h"])
|
||||
{
|
||||
switcher.Run(args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Summary[] summaries = switcher.Run(args.Length == 0 ? ["--filter", "*"] : args).ToArray();
|
||||
// BenchmarkDotNet can return normally after validation, build, or execution
|
||||
// failures. Do not let CI mistake an empty or failed run for a passing smoke test.
|
||||
if (summaries.Length == 0 || summaries.Any(summary =>
|
||||
summary.HasCriticalValidationErrors || summary.Reports.Length == 0 ||
|
||||
summary.Reports.Any(report => !report.Success || report.ResultStatistics is null)))
|
||||
{
|
||||
Console.Error.WriteLine("Benchmark run failed or produced no measurements.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user