---
title: "Discussion on \"Speed up your python code by caching\" | Hashnode"
description: "Discussion on \"Speed up your python code by caching\". Let's say you have a function that is a super-slow function. Not sure how you can find which is a super slow function? Measure it with this.\nNow there is now way you can optimize the function, what you can do instead is that you can store results fro..."
url: "https://hashnode.com/posts/cache-your-code/5fe703346a146422083cd0e7"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Speed up your python code by caching](https://publications.theroyakash.com/cache-your-code?utm_source=hashnode&utm_medium=feed)

Let's say you have a function that is a super-slow function. Not sure how you can find which is a super slow function? Measure it with this. Now there is now way you can optimize the function, what you can do instead is that you can store results fro...

Dec 26, 2020 · 6 min read · publications.theroyakash.com  
Updated Jan 8, 2021  
Read the article

-   [#python](https://hashnode.com/tag/python)
-   [#caching](https://hashnode.com/tag/caching)
-   [#speed](https://hashnode.com/tag/speed)
-   [#life-hack](https://hashnode.com/tag/life-hack)
-   [#christmashackathon](https://hashnode.com/tag/christmashackathon)

## Written by

[theroyakash](https://hashnode.com/@theroyakash)

@theroyakash · Chennai, India

  
[View profile](https://hashnode.com/@theroyakash)

Computer Scientist

I go by theroyakash on the internet. I am a computer scientist, with research in high performance algorithms, data structures, distributed systems, and beyond. See my work searching google for theroyakash.

## Responses 2

Markdown supported · ⌘↵ to post

[Sébastien Portebois](https://hashnode.com/@sportebois)

[Dec 26, 2020](https://hashnode.com/posts/cache-your-code/5fe703346a146422083cd0e7/comment/5fe7cd976a146422083cdaff)

Did you try timing it against the built-in cache functions Python provides?

eg [docs.python.org/3/library/functools.html](https://docs.python.org/3/library/functools.html)

It’s indeed a good way to introduce how to create custom decorators, but for this caching purpose, I’d believe the built-in implementation will be faster.

[theroyakash](https://hashnode.com/@theroyakash)

[Dec 27, 2020](https://hashnode.com/posts/cache-your-code/5fe703346a146422083cd0e7/comment/5fe7cd976a146422083cdaff#reply-5fe85bb4999b3f21e31585c5)

The built in caching function has a limited storage space and unused cached elements are evicted with LRU policy. As both uses a dictionary to store the data cache access time would be same O(1) constant

[Luiz Filipe da Silva](https://hashnode.com/@luizfilipezs)

[Dec 26, 2020](https://hashnode.com/posts/cache-your-code/5fe703346a146422083cd0e7/comment/5fe735796a146422083cd36c)

Great one! I'll be using this decorator in future needs. Thanks for sharing!

[theroyakash](https://hashnode.com/@theroyakash)

[Dec 26, 2020](https://hashnode.com/posts/cache-your-code/5fe703346a146422083cd0e7/comment/5fe735796a146422083cd36c#reply-5fe735b2999b3f21e3157763)

Glad you liked it
