Last active
August 7, 2019 17:09
-
-
Save mratsim/8c1afd5fdffeb8eb6b9508328f270455 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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