-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctionCaching.py
49 lines (36 loc) · 1.13 KB
/
functionCaching.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
39
40
41
42
43
44
45
46
47
48
49
import time
from functools import lru_cache
@lru_cache(maxsize=32) ## MAXSIZE MEANS IT REMEMBERS LAST n(Here=32) VALUES and caches those.
def some_work(n):
#Some task taking n seconds
time.sleep(n)
return n
if __name__ == '__main__':
print("Now running some work")
some_work(3)
some_work(1)
some_work(6)
some_work(2)
print("Done... Calling again")
input()
some_work(3)
print("Called again")
#For better understanding ------------------------------------------------>
class Record:
__dct = None
def __init__(self, dict):
self.dct = dict
@lru_cache(maxsize = int(input("Please enter the cache size: \n")))
def lastName (self, firstName):
time.sleep(3)
if firstName in self.dct:
return self.dct[firstName]
else:
return "Not Found"
if __name__ == "__main__" :
dict = {"Harry": "Skillf", "Miracle": "Jr", "Obama": "Champion", "Rohan": "Friend"}
record = Record(dict)
print(record.lastName("Harry"))
print(record.lastName("Miracle"))
print(record.lastName("Obama"))
print(record.lastName("Miracle"))