In this video we delve into caching in Python, exploring how it works and its potential to improve performance.
To demonstrate caching, we present an example where a slow function is repeatedly called with the same arguments, resulting in a 2-second runtime each time (simulating "slowness" by using time.sleep).
By implementing caching, we store the results in a cache dictionary, associating them with the corresponding arguments. Subsequent function calls with the same arguments retrieve the results from the cache, eliminating the need for computing the values again which significantly boosts performance.
Python offers the built-in functools.cache decorator (lru_cache before) to implement caching seamlessly. By applying this decorator to a function, the caching mechanism is automatically incorporated. We check the source code of this decorator, which at its core uses a cache dictionary as well 💡
Additionally, external packages like request_cache provide tools to cache network requests using a local (SQLite) database, enabling faster subsequent retrieval of data. We show how we have implemented this in our pybites-search tool and how it speeds up searching Pybites content 😍
Using caching we can enhance performance and create more efficient Python programs. We hope you'll embrace it and please comment below when you do ... 💪🔥
Links:
Explaining cache code: https://gist.github.com/bbelderbos/d2...
Functools cache example code: https://gist.github.com/bbelderbos/c2...
Pybites tip - requests-cache: https://codechalleng.es/tips/caching-...
Our use of requests-cache in pybites-search: https://github.com/PyBites-Open-Sourc...
---
Learn more about Pybites / what we do: https://pybit.es
Get our weekly developer / mindset emails: https://pybit.es/friends/
Join our Slack community: https://pybit.es/community/
And last but not least, we appreciate any feedback to make our YouTube content better 💡
https://form.jotform.com/230941798741062 🙏
Nesta página do site você pode assistir ao vídeo on-line Boost Python Code Performance with Caching: Exploring Techniques and Tools duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Pybites 17 Maio 2023, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 1,036 vezes e gostou 49 espectadores. Boa visualização!