PermalinkIntroduction
Welcome to my 92nd blog post on the Python journey. On day 92, I learned about the concept of function caching in Python. This concept can be used to improve the performance of the program by storing the results returned and reusing them instead of recomputing the function every time. Let's dive into more details and understand the function caching works in Python.
So let's get started......
PermalinkFunction Caching
It is used to improve the performance of a program by storing the results of a function call.
This helps us to reuse the results instead of recomputing the function every time.
Function Caching can be used when a function is computationally expensive i.e it may take a long time to run, or when the inputs to the function are unlikely to change frequently.
Function caching in Python is done using the functools.lru_cache decorator.
Example -
from functools import lru_cache
import time
@lru_cache(maxsize=None)
def fx(n):
time.sleep(5)
return n*5
print(fx(20))
print("done for 20")
print(fx(2))
print("done for 2")
print(fx(6))
print("done for 6")
print(fx(20))
print("done for 20")
print(fx(2))
print("done for 2")
print(fx(6))
print("done for 6")
print(fx(61))
print("done for 61")
In the above example, the functools.lru_cache
decorator is used to cache the results of the fx(n)
function. The maxsize parameter is used to specify the maximum number of results to cache. If maxsize
is set to None
, the cache will have an unlimited size.
PermalinkBenefits of Function Caching
Function caching can have a significant impact on the performance of a program, particularly for high-end computation functions. Here are the benefits of Function Caching in Python
We can avoid recomputing the results every time the function is called.
This can save a significant amount of time and computational resources.
Simplification of program code can be done by removing the need to manually cache the results of a function.
With the
functools.lru_cache
decorator, the caching is handled automatically, so we can focus on writing the core logic of your program.
PermalinkResources Used
You can watch the video of Day#92 by clicking on the below link 👇👇👇👇👇
PermalinkConclusion
Thanks, guys for going through this blog post. On day 92, I learned about Function caching as a technique to improve program performance by storing the results of a function so the results can be reused instead of recomputing them every time the function is called.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #92
See you in the next one.....
PermalinkAbout Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.
GitHub - github.com/CodeWithJain
Twitter - twitter.com/CodewithJain
Instagram - instagram.com/codewithjain
Youtube - youtube.com/@CodeWithCJ
Subscribe to our newsletter
Read articles from Chintan's Blog - @codewithjain directly inside your inbox. Subscribe to the newsletter, and don't miss out.