The following code computes the usual Fibonacci series number for a given n.
import
parseopt, strutils
proc fib(n: int): int =
if n < 2:
return 1
else:
return fib(n-1) + fib(n-2)
when isMainModule:
var n = 0
for kind, key, value in parseopt.getopt():
case kind
of parseopt.cmdArgument:
n = strutils.parseInt(key)
else:
echo "!! Error"
echo fib(n)
For n = 40, the above code takes about 8.9 seconds. On the same machine, under the same conditions, the equivalent Julia code takes less than 1.5 seconds. What makes Nimrod so slow in comparison? Thanks.
-- 0|0
Try building with -d:release
i.e. nimrod c -d:release fib.nim
Edit: Here's my results
fib 40
165580141
Time taken 0.335
Note I added:
import times
...
let clockBegin = epochTime()
echo fib(n)
echo "Time taken ", formatFloat(epochTime() - clockBegin, ffDecimal, precision = 3)
-- 0|0
To expand on Orion's answer, it can be useful to understand what precisely -d:release entails in order to only selectively turn the relevant options on and off for development and release.
By default, Nimrod compiles (1) without optimization, (2) with line number/stack frame information enabled, (3) with checks turned on, and (4) with dead code elimination turned off. You can turn them on/off selectively during development, and several may be useful for release code, too.
If you want to avoid typing in long lists of options, you can also put them in a configuration file and also make them conditional on other -d options; see the default config/nimrod.cfg for how -d:release and -d:quick work.
Thank you, Jehan, for the detailed description of the compiler flags and their mechanisms of operation. It is very useful.
These kinds of posts should be made sticky!
-- 0|0