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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #
# fichier main.py
#
from pyximport import install ; install()
import cython_fn
import time
import python_fn
#iterations
n = 1000000
# cal cython version
t0 = time.time()
sum_cython = cython_fn.cos_calc(n)
t1 = time.time()
t = t1-t0
print(f"Somme cython: {sum_cython:.2f}")
print(f"Durée : {t:.6f}")
# calc python version
t0 = time.time()
sum_python = python_fn.cos_calc(n)
t1 = time.time()
t = t1-t0
print(f"Somme python: {sum_python:.2f}")
print(f"Durée : {t:.6f}")
#
# fichier cython_fn.pyx
#
# cython : language_Level = 3
import math
def cy_fibo(int n):
cdef int a,b,i
a , b = 1, 1
for i in range(n):
a, b = a+b, a
return a
# version cython
def cos_calc(int n):
cdef double s = 0.0
cdef int i=0
for i in range(n):
s += (math.cos(i) + math.sin(i))
return s
#
# fichier python_fn.py
#
import math
def cos_calc(n : int):
s = 0.0
for i in range(n):
s += math.cos(i) + math.sin(i)
return s |