Python: Cache results of functions.
Assume that you have functions whose result remains the same every time they are called with the same parameters. Can we cache their…
Assume that you have functions whose result remains the same every time they are called with the same parameters. Can we cache their result? We can, and it is super easy! let’s see how!
You have a function called time_consuming_function. This function returns the sum of parameters a and b and needs about 10 seconds to execute; the result of the function is deterministic, which means that the result will be the same if the parameters are always the same.
time_consuming_function
def time_consuming_function(a,b):
time.sleep(10)
return a+bThis means that if we had a mechanism to cache results, the improvement would be significant! the result would instantly be available; this can be done using the cachetools.func library, which offers a handy decorator for our functions.
The parameters for the decorator are
- maxsize: The size of the cached results; if exceeded, it will choose the least recently used cached item to be discarded.
- ttl: Time for the cached items to live in the cache in seconds
#!/usr/bin/env python3
import cachetools.func
import time
@cachetools.func.ttl_cache(maxsize=10, ttl=1*60)
def time_consuming_function(a,b):
time.sleep(10)
return a+bExample
In this example we call the time_consuming_function twice and we measure the time that needs to be executed
#!/usr/bin/env python3
import cachetools.func
import time
@cachetools.func.ttl_cache(maxsize=10, ttl=1*60)
def time_consuming_function(a,b):
time.sleep(10)
return a+b
if __name__ == '__main__':
start = time.time()
print(time_consuming_function(a=1,b=1))
print(time.time()-start)
start = time.time()
print(time_consuming_function(a=1,b=1))
print(time.time()-start)We can see that the first execution took 10 secs as expected, the second execution took just some uSec because the result was cached.
kpatronas@nautilus:~$ ./c1.py
2
10.01044750213623
2
0.0002288818359375If we insert a sleep of 61 seconds between the two executions the second function will execute normally because the cache has been expired.
#!/usr/bin/env python3
import cachetools.func
import time
@cachetools.func.ttl_cache(maxsize=5, ttl=1*60)
def time_consuming_function(a,b):
time.sleep(10)
return a+b
if __name__ == '__main__':
start = time.time()
print(time_consuming_function(a=1,b=1))
print(time.time()-start)
print("Sleeping for 61 seconds")
time.sleep(61)
print("End sleep")
start = time.time()
print(time_consuming_function(a=1,b=1))
print(time.time()-start)kpatronas@nautilus:~$ ./c1.py
2
10.010467052459717
Sleeping for 61 seconds
End sleep
2
10.01061487197876Conclusion
Caching can be a performance boost in cases that can implemented, but be careful that it can be used only in functions where the same result is always expected by a function.
In Plain English 🚀
Thank you for being a part of the In Plain English community! Before you go:
- Be sure to clap and follow the writer ️👏️️
- Follow us: X | LinkedIn | YouTube | Discord | Newsletter
- Visit our other platforms: CoFeed | Differ
- More content at PlainEnglish.io