Boost Python Code Performance with Caching: Exploring Techniques and Tools

Published: 17 May 2023
on channel: Pybites
1,036
49

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 🙏


On this page of the site you can watch the video online Boost Python Code Performance with Caching: Exploring Techniques and Tools with a duration of hours minute second in good quality, which was uploaded by the user Pybites 17 May 2023, share the link with friends and acquaintances, this video has already been watched 1,036 times on youtube and it was liked by 49 viewers. Enjoy your viewing!