Created
February 21, 2020 18:43
-
-
Save FBosler/7f5597b6e308762048d252f53ef8e4fe to your computer and use it in GitHub Desktop.
functools.py
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
from functools import lru_cache | |
from datetime import datetime | |
@lru_cache(maxsize=None) | |
def fib_cache(n): | |
if n < 2: | |
return n | |
return fib_cache(n-1) + fib_cache(n-2) | |
def fib_no_cache(n): | |
if n < 2: | |
return n | |
return fib_no_cache(n-1) + fib_no_cache(n-2) | |
def timeit(func,samples): | |
start = datetime.now() | |
func(samples) | |
end = datetime.now() | |
return end-start |
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
UPPER = 40 | |
cached = [] | |
for i in range(0,UPPER): | |
cached.append(timeit(fib_cache,i)) | |
not_cached = [] | |
for i in range(0,UPPER): | |
not_cached.append(timeit(fib_no_cache,i)) |
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 pandas as pd | |
import seaborn as sns | |
_ = pd.DataFrame([cached,not_cached]).T | |
_.columns = ['cached','not_cached'] | |
_ = _.applymap(lambda x: x.value/1000000000) | |
sns.set() | |
g = sns.lineplot(data=_) | |
g.set_ylabel('time in seconds') | |
g.set_xlabel('samples') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment