Skip to content

Instantly share code, notes, and snippets.

@0atman
Last active April 12, 2020 04:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0atman/373573e1f91758263fc0ced5a5166f7b to your computer and use it in GitHub Desktop.
Save 0atman/373573e1f91758263fc0ced5a5166f7b to your computer and use it in GitHub Desktop.
IMO Nim is an advanced, complied superset of python
import math, strformat, times
func fib(n: int): int =
if n <= 2:
return 1
else:
return fib(n - 1) + fib(n - 2)
when isMainModule:
let x = 47
let start = epochTime()
let res = fib(x)
let elapsed = epochtime() - start
echo(&"Nim Computed fib({x})={res} in {elapsed} seconds")
import time
def fib(n: int) -> int:
if n <= 2:
return 1
else:
return fib(n - 1) + fib(n - 2)
if __name__ == "__main__":
x = 47
start = time.time()
res = fib(x)
elapsed = time.time() - start
print(f"Py3 Computed fib({x})={res} in {elapsed} seconds")
@0atman
Copy link
Author

0atman commented Apr 6, 2020

Code taken from https://robert-mcdermott.gitlab.io/posts/speeding-up-python-with-nim/

Python ran in 504.55 seconds
Nim ran in 4.63 seconds, same speed as C, and faster than any other language he tested:

C          Computed fib(47)=2971215073 in 4.58 seconds
Java       Computed fib(47)=2971215073 in 7.74 seconds
Go         Computed fib(47)=2971215073 in 10.94 seconds
JavaScript Computed fib(47)=2971215073 in 21.384 seconds
PyPy       Computed fib(47)=2971215073 in 93.63 seconds
Ruby       Computed fib(47)=2971215073 in 191.57 seconds
Python3    Computed fib(47)=2971215073 in 504.55 seconds
Perl5      Computed fib(47)=2971215073 in 980.24 seconds
R          Computed fib(47)=2971215073 in 2734.70 seconds

I'd imagine rust is equivalent to the nim and c speed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment