Skip to content

Instantly share code, notes, and snippets.

@jahav
Created June 8, 2022 21:18
Show Gist options
  • Save jahav/677328404774d5551ce1174373da3237 to your computer and use it in GitHub Desktop.
Save jahav/677328404774d5551ce1174373da3237 to your computer and use it in GitHub Desktop.
A benchmark to compare how expensive an exception is compare to a returned value.
/*
| Method | Mean | Error | StdDev |
|-------------- |-------------:|-----------:|-----------:|
| ReturnValue | 42.18 ns | 0.736 ns | 0.689 ns |
| ExceptionPath | 24,777.17 ns | 199.816 ns | 177.131 ns |
*/
public class FormulaExceptionVsReturnBenchmark
{
private string formExc = "=ABS(-1)/ABS(0)";
private string formRet = "=ABS(-1)/ABS(1)";
public FormulaExceptionVsReturnBenchmark()
{
try
{
XLWorkbook.EvaluateExpr(formExc);
}
catch { }
XLWorkbook.EvaluateExpr(formRet);
}
[BenchmarkDotNet.Attributes.Benchmark]
public object ReturnValue()
{
return XLWorkbook.EvaluateExpr(formRet);
}
[BenchmarkDotNet.Attributes.Benchmark]
public object ExceptionPath()
{
try
{
return XLWorkbook.EvaluateExpr(formExc);
}
catch (CalcEngineException)
{
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment