following example is even more drastic, 3x speedup over C++ (in case where
there are a lot of thrown exceptions)
import std / [times, monotimes, stats, strutils]
const depthRaise = 100
type MyExcept = object of CatchableError
proc fib(m: string, depth = 0): int =
if depth == depthRaise: raise newException(MyExcept, "gook")
let n = parseInt(m)
doAssert n > 0
if n <= 1: return 1
else: return fib($(n-1), depth + 1) + fib($(n-2), depth + 1)
var numCaught = 0
proc benchFun()=
try: echo fib($(depthRaise+1))
except MyExcept: numCaught.inc
when true:
var r: RunningStat
for iterations in 1..5:
let start = getMonoTime()
for i in 0 ..< 100000:
benchFun()
echo numCaught
r.push float((getMonoTime() - start).inMilliseconds)
echo r
Run