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
|
import time
def reuse_(num): def outer(func): def inner(*args): for i in range(num): result = func(*args) return result return inner return outer
list_x = range(999)
@reuse_(10**3) def f(a,n,x): p = 0 for i in range(n+1): p += (a[i]*(x**i)) return p start_1 = time.time() f_value = f(list_x,888,888) print(f_value)
print('第一种方法运行时间:%d'%(time.time()-start_1))
@reuse_(10**3) def ff(a,n,x): p = a[n] for i in range(n,0,-1): p = a[i-1] + x*p return p start_2 = time.time() ff_value = ff(list_x,888,888) print(ff_value)
print('第二种方法运行时间:%d'%(time.time()-start_2))
|