Performance hacks for faster Python code
51–60 of 65 posts
Re: Performance hacks for faster Python code
#52Re: Performance hacks for faster Python code
#53a smart hack for performance is don't use python
Re: Performance hacks for faster Python code
#54I remember the day I realized how much I dislike python. I have never had it click for me, despite writing it in and off since the python 2.0. There are always some new arbitrary places for it to bite you, and it always feels a little yucky. And then one day I saw something like this:
# b is not defined here
if blahblah():
b = gronk()
# might raise an exception
do_stuff(b)
That tingles in all the wrong places. Ruby has other issues, but the core is still feels elegant. I still prefer scheme, though.Edit: and maybe someone can explain to me why anyone would make it so that the simplest way to iterate through a collection is not the fastest? This is the case for most languages, but it still feels dumb. Just allow a slightly more obtuse syntax like
for a in b
and make that case just as fast as doing it with a while loop. The iterator protocol is imho for when we frankly don't know/care what we are iterating over, or when we elwant to be generic, or when the data structure can't expose the most efficient way of doing it (like a tree). There is no reason why for a in b: should be slower than while when b is a list or string.Re: Performance hacks for faster Python code
#55Isn't this pretty obvious if you spent more than three weeks writing python? There are lots tricks, but these listed here seem relevant to all languages. There is an almost an incountable amount of ways to write bad and slow python code that are not as obvious. I remember the day I realized how much I dislike python. I have never had it click for me, despite writing it in and off since the python 2.0. There are alway…
The hack for using Math instead of operators seems dumb.
Re: Performance hacks for faster Python code
#56Isn't this pretty obvious if you spent more than three weeks writing python? There are lots tricks, but these listed here seem relevant to all languages. There is an almost an incountable amount of ways to write bad and slow python code that are not as obvious. I remember the day I realized how much I dislike python. I have never had it click for me, despite writing it in and off since the python 2.0. There are alway…
That said, agreed that this is weird. Python makes some really unintuitive choices regarding scope.
Re: Performance hacks for faster Python code
#57Isn't this pretty obvious if you spent more than three weeks writing python? There are lots tricks, but these listed here seem relevant to all languages. There is an almost an incountable amount of ways to write bad and slow python code that are not as obvious. I remember the day I realized how much I dislike python. I have never had it click for me, despite writing it in and off since the python 2.0. There are alway…
Try as I might, I can't get that to raise an exception. That said, agreed that this is weird. Python makes some really unintuitive choices regarding scope.
Re: Performance hacks for faster Python code
#58Isn't this pretty obvious if you spent more than three weeks writing python? There are lots tricks, but these listed here seem relevant to all languages. There is an almost an incountable amount of ways to write bad and slow python code that are not as obvious. I remember the day I realized how much I dislike python. I have never had it click for me, despite writing it in and off since the python 2.0. There are alway…
Try as I might, I can't get that to raise an exception. That said, agreed that this is weird. Python makes some really unintuitive choices regarding scope.
if input() == "dynamic scope?":
defined = "happyhappy"
print(defined)Re: Performance hacks for faster Python code
#59Maybe 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
#60Earlier quoted context omitted.
swap with last element then truncate at the end
Yes, you can do this if you don't care about order, and avoid the performance degradation. But it's even more complex. Or if you do care about order, you can emulate the C++ "erase-remove" idiom, by keeping track of separate "read" and "write" positions in the source, iterating until "read" reaches the end, and only incrementing "write" for elements that are kept; and then doing a single `del` of a slice at the end.…
It will also probably be significantly slower than just copying the vector.