Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Python/Memoization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
def add80(n):
print('Long time')
return n+80

print(add80(5))
print(add80(5))

#Memoization 1

cache = {}

def memoizedadd80(n):
if n in cache:
return n + 80
else:
print('Long time')
cache[n] = n+80
return cache[n]

print(memoizedadd80(6))
print(memoizedadd80(6))

#Memoization 2
def memoizedadd80():
cache = {}

def memoized(n):
if n in cache:
return n + 80
else:
print('Long time')
cache[n] = n+80
return cache[n]
return memoized

memo = memoizedadd80()
print(memo(7))
print(memo(7))



# https://docs.python.org/3.3/library/functools.html --> Doc for lru_cache

from functools import lru_cache

@lru_cache(maxsize = 1000)
def memoized2add80(n):
return n + 80


print(memoized2add80(8))
print(memoized2add80(8))
print(memoized2add80.cache_info())