Created
March 7, 2016 01:19
-
-
Save drhanlau/7ba55e45693f294f564c to your computer and use it in GitHub Desktop.
decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci(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 | |
t = Timer('fibonacci(8)', 'from __main__ import fibonacci') | |
print(t.timeit()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
known = {0:0, 1:1} | |
def fibonacci(n): | |
assert(n >= 0), 'n must be >= 0' | |
if n in known: | |
return known[n] | |
res = fibonacci(n-1) + fibonacci(n-2) | |
known[n] = res | |
return res | |
if __name__ == '__main__': | |
from timeit import Timer | |
t = Timer('fibonacci(100)', 'from __main__ import fibonacci') | |
print(t.timeit()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
def memoize(fn): | |
known = dict() | |
@functools.wraps(fn) | |
def memoizer(*args): | |
if args not in known: | |
known[args] = fn(*args) | |
return known[args] | |
return memoizer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
known_sum = {0:0} | |
def nsum(n): | |
assert(n >= 0), 'n must be >= 0' | |
if n in known_sum: | |
return known_sum[n] | |
res = n + nsum(n-1) | |
known_sum[n] = res | |
return res |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@memoize | |
def nsum(n): | |
'''Returns the sum of the first n numbers''' | |
assert(n >= 0), 'n must be >= 0' | |
return 0 if n == 0 else n + nsum(n-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment