Skip to content

Commit c645808

Browse files
committed
2019.1.30 add more fib examples
1 parent 7007d4d commit c645808

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

fib/fib.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
import math, strformat, times
4+
5+
proc fib(n: int): int =
6+
if n <= 2:
7+
return 1
8+
else:
9+
return fib(n - 1) + fib(n - 2)
10+
11+
when isMainModule:
12+
let x = 47
13+
let start = epochTime()
14+
let res = fib(x)
15+
let elapsed = (epochtime() - start).round(2)
16+
stderr.writeLine(&"Nim Computed fib({x})={res} in {elapsed} seconds")

fib/fib.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def cache_fib(n, _cache={}):
1919

2020
if __name__ == "__main__":
2121
x = 47
22+
start = time.time()
23+
res = fib(x)
24+
elapsed = time.time() - start
25+
print("Python Computed fib(%s)=%s in %0.8f seconds" % (x, res, elapsed))
26+
2227
start = time.time()
2328
res = cache_fib(x)
2429
elapsed = time.time() - start

fib/fib_nimpy.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import nimpy
2+
3+
proc fib(n: int): int {.exportpy.} =
4+
if n <= 2:
5+
return 1
6+
else:
7+
return fib(n - 1) + fib(n - 2)

fib/fib_nimpy.so

58 KB
Binary file not shown.

fib/nim_fib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import time
2+
from fib_nimpy import fib
3+
4+
if __name__ == "__main__":
5+
x = 30
6+
start = time.time()
7+
res = fib(x)
8+
elapsed = time.time() - start
9+
print("Py3+Nim Computed fib(%s)=%s in %0.8f seconds" % (x, res, elapsed))

0 commit comments

Comments
 (0)