-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.py
29 lines (18 loc) · 786 Bytes
/
t.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import timeit
import random
from c_fib import c_fib
from go_fib import go_fib
NUM = random.randint(30, 35)
def fib(n: int) -> int:
if n == 0:
return 0
elif n == 1:
return 1
return fib(n-1) + fib(n-2)
if __name__ == '__main__':
executions = 10
print(f"Running benchmark for fibonacci sequence number : {NUM}, with {executions} executions")
print(f"python: {timeit.timeit('fib(NUM)', number=executions, globals=locals())}")
print(f"c-extension: {timeit.timeit('c_fib(NUM)', number=executions, globals=locals())}")
print(f"go-extension: {timeit.timeit('go_fib(NUM)', number=executions, globals=locals())}")
assert fib(NUM) == c_fib(NUM) == go_fib(NUM), f"Implementation mismatch... {fib(NUM)=}, {c_fib(NUM)=}, {go_fib(NUM)=}"