Live data from Hacker News

Performance hacks for faster Python code

blog.jetbrains.com

11–20 of 65 posts

Re: Performance hacks for faster Python code

#11

There's some genuinely interesting tips in here, but #10 is for sure just padding so they could call the article "10 Hacks" haha. Everything else is at least somewhat Python specific, but "Hack 10: Avoid repeated function calls in loops" is just applicable to anything.

I agree. I can't imagine anybody would call a function that returns the same result in a loop like that. There are plenty more optimizations they could come up with. In fact, there are a couple of books https://www.oreilly.com/library/view/high-performance-python... for instance. Didn't want to link the one on Amazon

Re: Performance hacks for faster Python code

#14

There's some genuinely interesting tips in here, but #10 is for sure just padding so they could call the article "10 Hacks" haha. Everything else is at least somewhat Python specific, but "Hack 10: Avoid repeated function calls in loops" is just applicable to anything.

Yeah, 10 felt like it was written by ai.

Re: Performance hacks for faster Python code

#15
post #7

Some helpful guidelines, but it's 2025 and people still use time.time and no stats with their benchmarks :( In general I feel like these kind of benchmarks might change for each python version, so some caveats might apply.

Perhaps you could suggest what should be used instead of time.time

Re: Performance hacks for faster Python code

#18
post #2

Maybe also knowing when not to use python, or finding a solution in python that uses C/rust/etc underneath.

It's kinda funny how uv is written in Rust and many Python libraries where performance is expected to matter (NumPy, Pandas, PyTorch, re, etc.) are implemented in C. Even if you call into fast code from Python you still have to contend with the GIL which I find very limiting for anything resembling performance.

Re: Performance hacks for faster Python code

#19
post #9

> Copying large objects like lists […] can be costly in both time and memory. > modify[ing] objects in place […] improves performance by avoiding the overhead of allocating and populating new structures. AFAIK the poor performance of list copies (demonstrated in the article by a million-element list taking 10ms) doesn’t come from memory allocation nor from copying the contents of the list itself (in this case, a mill…

Yeah. A more nuanced approach is that you should copy things that don't consist of lots and lots of references to other things, and you should mutate things that are mostly references to other structures.

Which means, eventually, designing your data structures so you generally have two types of structures: one which isn't full of pointers, and one which mostly is.

Re: Performance hacks for faster Python code

#20
post #2

Maybe also knowing when not to use python, or finding a solution in python that uses C/rust/etc underneath.

People are making fun of this statement here / are being sarcastic. But it's a totally legit suggestion. If you know in advance, you are going to make something where performance matters, strongly consider using something other than one of the slowest languages of them all.
Post reply on HN