upvote
Python has optimized string appending in the form of:

s = s + "foo"

and

s += "foo"

Since 2005 with the release of Python 2.4:

https://docs.python.org/3/whatsnew/2.4.html#optimizations

reply
I’m not a fan of this kind of “fancy” optimization anyway.

It’s too fragile. I may make some innocuous change, now the compiler cannot recognize the pattern and performance falls off the cliff.

I’d rather have the reliabile performance than the absolute fastest possible result. Then if there’s an issue I can catch and fix it reliably with profiling, not deal with a heisenbug based on whether the compiler can match the pattern.

reply