Live data from Hacker News

Speeding up function calls with lru_cache in Python

hackeregg.github.io

21–30 of 85 posts

Re: Speeding up function calls with lru_cache in Python

#22
This is neat and I learned something new. TLDR: use the functools.lru_cache decorator to add caching to slow functions.

I must admit I was hoping for a general approach to speeding up all function calls in python. Functions are the primary mechanism for abstraction and yet calls are relatively heavy in their own right. It would be neat if python had a way to do automatic inlining or some such optimization so that I could have my abstractions but avoid the performance hit of a function call (even at the expense of more byte code).

Re: Speeding up function calls with lru_cache in Python

#24
post #5

Instead of the last quote in the article, I prefer this one (got it from [0]) >"There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors." – Martin Fowler And there's plenty of similar articles, for example [1] [2] [0] https://www.mediawiki.org/wiki/Naming_things [1] https://dbader.org/blog/python-memoization [2] https://mike.place/2016/memoization/

Thanks for similar articles. I am aware of similar articles and that's why I have kept it short and simple. I will add these as references, later.

I have enjoyed reading Python Tricks[0] book by Dan bader (author of https://dbader.org/blog/python-memoization ). It's awesome. Might be outdated but I still recommend it to any body who has just started learning python.

[0] https://realpython.com/products/python-tricks-book/

Re: Speeding up function calls with lru_cache in Python

#26

Maybe I'm abusing lru_cache but another use for it is debouncing. We had a chatbot that polls a server and sends notifications, but due to clock skew it would sometimes send two notifications. So I just added the lru_cache decorator to the send(username, message) function to prevent that.

That sounds very interesting. Can you detail the problem a bit (I'm not sure I understand how clock skew affects python code) and how lru_cache decorator fixed it?

Re: Speeding up function calls with lru_cache in Python

#27

Maybe I'm abusing lru_cache but another use for it is debouncing. We had a chatbot that polls a server and sends notifications, but due to clock skew it would sometimes send two notifications. So I just added the lru_cache decorator to the send(username, message) function to prevent that.

Are you not using a message id of some sort?

Re: Speeding up function calls with lru_cache in Python

#29
post #5

Instead of the last quote in the article, I prefer this one (got it from [0]) >"There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors." – Martin Fowler And there's plenty of similar articles, for example [1] [2] [0] https://www.mediawiki.org/wiki/Naming_things [1] https://dbader.org/blog/python-memoization [2] https://mike.place/2016/memoization/

Even in this version, the quote still feels incomplete without someone shouting "Concurrency!" while it is delivered...

Re: Speeding up function calls with lru_cache in Python

#30

Earlier quoted context omitted.

Author here. Sorry, If it feels misleading. I have no intention to mislead anybody. If you can suggest alternative title, I am happy to change title.

Thanks for article Just a side note: with Fibonacci + caching it solidly become Dynamic programming problem so Time complexity reduces from quadratic to o(n), IIRC . There is a whole class of problems where recursion + memoization(caching) = Top Down Dynamic programming , The other way to Increase performance and actually reduce call stack in these class of problems including Fibonacci would be Bottom Up Dynamic Prog…

the simple recursive solution is actually O(fib(n)), which is exponential
Post reply on HN