Skip to content

Instantly share code, notes, and snippets.

@jahav
Last active June 8, 2022 18:35
Show Gist options
  • Save jahav/2d621605230a54a23a84019bac5e2de4 to your computer and use it in GitHub Desktop.
Save jahav/2d621605230a54a23a84019bac5e2de4 to your computer and use it in GitHub Desktop.
ClosedXML - Exception vs Return value benchmark
/*
My results
| Method | Mean | Error | StdDev |
|-------------- |-------------:|-----------:|-----------:|
| ReturnValue | 61.06 ns | 1.304 ns | 1.281 ns |
| ExceptionPath | 31,077.00 ns | 371.773 ns | 347.757 ns |
*/
public class FormulaExceptionVsReturnBenchmark : IDisposable
{
private readonly IXLWorkbook _workbook;
private IXLCell _exceptionCell;
private IXLCell _returnCell;
public FormulaExceptionVsReturnBenchmark()
{
_workbook = new XLWorkbook();
var worksheet = _workbook.AddWorksheet();
_exceptionCell = worksheet.Cell("A1");
_exceptionCell.FormulaA1 = "=ABS(-1)/ABS(0)";
// Force evaluation
try { var _ = _exceptionCell.Value; } catch { }
_returnCell = worksheet.Cell("A2");
_returnCell.FormulaA1 = "=ABS(-1)/ABS(1)";
// Force evaluation
_ = _returnCell.Value;
}
[BenchmarkDotNet.Attributes.Benchmark]
public object ReturnValue()
{
return _returnCell.Value;
}
[BenchmarkDotNet.Attributes.Benchmark]
public object ExceptionPath()
{
try
{
return _exceptionCell.Value;
}
catch (CalcEngineException)
{
return null;
}
}
public void Dispose() => _workbook.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment