-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecorator__.py
38 lines (30 loc) · 1.03 KB
/
Decorator__.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
from functools import wraps
def memoize(fn):
known = dict()
@wraps(fn)
def memoizer(*args):
if args not in known:
known[args] = fn(*args)
return known[args]
return memoizer
@memoize
def nsum(n):
'''返回前n个数字的和'''
assert(n >= 0), 'n must be <= 0'
return 0 if n == 0 else n + nsum(n - 1)
@memoize
def fibonacci(n):
'''返回斐波那契数列的第n个数'''
assert(n >= 0), 'n must be >= 0'
return n if n in (0, 1) else fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == '__main__':
from timeit import Timer
measure = [{'exec': 'fibonacci(100)', 'import': 'fibonacci', 'func': fibonacci}, {
'exec': 'nsum(200)', 'import': 'nsum', 'func': nsum}]
for m in measure:
t = Timer(
'{}'.format(
m['exec']), 'from __main__ import {}'.format(
m['import']))
print('name: {}, doc: {}, executing: {}, time: {}'.format(
m['func'].__name__, m['func'].__doc__, m['exec'], t.timeit()))