-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcython_wrapper_speedup.py
executable file
·42 lines (36 loc) · 1.32 KB
/
cython_wrapper_speedup.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# coding=utf-8
""" Python wrapper to time the Cython implementation for computing the nth fibonacci number
in a non-recursive fashion.
"""
from fib_python import compute_fibonacci
from cyfib import compute_fibonacci_wrapper
if __name__ == '__main__':
import sys
import timeit
n = 20
try:
n = int(sys.argv[1])
except Exception:
pass
number_of_times = 100000
try:
number_of_times = int(sys.argv[2])
except Exception:
pass
fib_py = compute_fibonacci(n)
fib_cy = compute_fibonacci_wrapper(n)
if fib_py != fib_cy:
raise(ValueError(fib_cy))
py_tot = timeit.timeit("compute_fibonacci({})".format(n),
setup="from fib_python import compute_fibonacci",
number=number_of_times)
cy_tot = timeit.timeit("compute_fibonacci_wrapper({})".format(n),
setup="from cyfib import compute_fibonacci_wrapper",
number=number_of_times)
py_avg = py_tot / number_of_times
cy_avg = cy_tot / number_of_times
print("fib({}) = {}".format(n, fib_py))
print("Python average time: {0:.2g}".format(py_avg))
print("Cython Wrapper average time: {0:.2g}".format(cy_avg))
print("Cython Wrapper speedup: {0:.2g} times".format(py_avg/cy_avg))