Boost Python Code Performance with Caching: Exploring Techniques and Tools

Опубликовано: 17 Май 2023
на канале: 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 🙏


Смотрите видео Boost Python Code Performance with Caching: Exploring Techniques and Tools онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Pybites 17 Май 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 1,03 раз и оно понравилось 4 людям.