In this video I present a super easy (for beginners too) method to dramatically speed up certain function calls in your Python code. It is something that I myself quite recently discovered. It is a concept that is super fundamental to Computer Science and is implemented across both hardware and software in various levels of abstraction.
The answer is CACHING!!!
We will be using the @cache function from the functools package present in the Python standard library. This decorator function enables us to cache the results of a Python function call saving us precious time by simply looking up the result on subsequent function calls, instead of recomputing the result.
I'll be doing a super fast explanation on what caching is, then I'll be demonstrating the use of @cache in a function call and show how it works.
For this, we need an API, which we will code super fast using Fastapi.
Next, we will look at an extension of this and what happens if your data source is constantly changing (live data sources) and how the cache copes with this (we use the third party cachetools package and use the TTL parameter to invalidate the cache after a specified amount of seconds).
Source code:
[ Ссылка ]
00:00 intro
00:10 caching
00:22 api requests without caching
00:35 api requests with caching
01:31 code walkthrough: fastapi
02:14 code walkthrough: application
03:30 run fastapi app
03:52 results without caching
04:00 results with caching using @cache
04:16 problem of live data sources
05:05 cache invalidation using cachetools package
05:25 code refactoring to simulate time changes
06:09 wrong results with @cache
06:40 fix by implementing cachetools
07:24 correct results using cachetools implementation
07:37 End
Please consider liking if you found the video useful and subscribe for more!
Subscribe here: [ Ссылка ]
How To Massively Speed Up Your Python Code
Теги
pythoncachingpython cachingcaching in pythonpython performancepython speed boostspeed up python codefast pythonoptimize python codefunctoolspython functoolspython programmingpython tutorialpython tutorial for beginnerscachecache in computer sciencepython functools tutorialpython cache tutorialimplement cache in pythoncaching pythoncachetools packagefastapifastapi python