Live data from Hacker News

Speeding up function calls with lru_cache in Python

hackeregg.github.io

31–40 of 85 posts

Re: Speeding up function calls with lru_cache in Python

#32

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?

Essentially the OP is using the decorator to prevent the function call from ever being run more than one time. It’s at most once.

Re: Speeding up function calls with lru_cache in Python

#33
post #10

Earlier quoted context omitted.

It is, though. It doesn’t speed up function execution, but it literally speeds up the function call.

It only speeds up calls to pure (side-effect free) functions, that have been executed before with these exact parameters and where the result is still cached. All other calls get slower. Also, there's an argument to be made that "function call" is the overhead involved in moving control from the call site to the callee (which can be significant in interpreted languages such as Python). That's at least how I interpret…

It speeds up recursive functions that haven’t been called before with the same arguments. In this case, it caches all the intermediate results that would otherwise have to be recalculated.

Re: Speeding up function calls with lru_cache in Python

#35

Caching the result is not speeding up function calls.

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.

"python memoization in one line"

Edit: I don't actually have much of a problem with the article headline as it is now. It depends a lot on your target audience! For Hacker News, yeah, we know what memoization is because we learned it in CS 102, right after we learned about recursion.

But for a lot of people who would get the most value from the article, the word "memoization" isn't going to mean much, and wouldn't read the article.

Maybe something like: "Memoization in Python, or how I learned to speed up function calls in just one line"

Re: Speeding up function calls with lru_cache in Python

#36
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/

[deleted]

Re: Speeding up function calls with lru_cache in Python

#39
This technique is called Memoization [0]

Here is an implementation of a memoize decorator in Python that will support all inputs [1]. You'd have to modify to not use all the Pylons framework stuff.

[0] https://en.wikipedia.org/wiki/Memoization

[1] https://github.com/reddit-archive/reddit/blob/master/r2/r2/l...

Re: Speeding up function calls with lru_cache in Python

#40

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 c…

You might be interested in pypy or nuitka
Post reply on HN