Skip to content

Instantly share code, notes, and snippets.

@mratsim
Last active August 7, 2019 17:09
Show Gist options
  • Save mratsim/8c1afd5fdffeb8eb6b9508328f270455 to your computer and use it in GitHub Desktop.
Save mratsim/8c1afd5fdffeb8eb6b9508328f270455 to your computer and use it in GitHub Desktop.
import
# STD lib
os, strutils, threadpool, strformat
# bench
# ../wtime
# Using Nim's standard threadpool
# Compile with "nim c --threads:on -d:release -d:danger --outdir:build benchmarks/fibonacci/stdnim_fib.nim"
#
# Note: it breaks at fib 16.
proc parfib(n: uint64): uint64 =
if n < 2: # Note: be sure to compare n<2 -> return n
return n # instead of n<=2 -> return 1
let x = spawn parfib(n-1)
let y = parfib(n-2)
return ^x + y
proc main() =
if paramCount() != 1:
echo "Usage: fib <n-th fibonacci number requested>"
quit 0
let n = paramStr(1).parseUInt.uint64
# let start = Wtime_msec()
let f = parfib(n)
# let stop = Wtime_msec()
echo "Result: ", f
# echo &"Elapsed wall time: {stop-start:.2} ms"
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment