Created
June 8, 2022 21:18
-
-
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.
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
/* | |
| 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