Message347412
Eric is correct that this is a CPython optimization, it is not a language feature.
Furthermore, it is an optimization that can be affected by rather subtle factors such as the operating system memory management. Here is a thread demonstrating that code that relied on this optimization ran fast on Linux and slow on Windows, literally hundreds of times slower than other tools:
https://mail.python.org/archives/list/python-dev@python.org/thread/EQO6HDZXHFCP4DBEE5Q7MYLHPJMGJ7DQ/
If you prefer the old mailman archives, here are a few relevant posts:
https://mail.python.org/pipermail/python-dev/2009-August/091125.html
https://mail.python.org/pipermail/python-dev/2009-September/091582.html
https://mail.python.org/pipermail/python-dev/2009-September/091592.html
Best practice is to *not* rely on this optimization, but to use str.join. That ensures that:
- your code remains fast if run on alternate implementations;
- your code remains fast if the OS memory management changes;
- your code remains fast if you change your code slightly.
For example, I expect both of the following to show the documented quadratic behaviour:
- Prepending instead of appending
- keeping two or more references to the string being appended to
partials = []
current = ''
for s in list_of_strings:
partials.append(current)
current += s
There may be others.
Perhaps we ought to document this optimization specifically so we can suggest that it is intended for improving performance of casual scripts, and that *best practice* for professional quality code remains str.join. |
|
Date |
User |
Action |
Args |
2019-07-06 01:14:12 | steven.daprano | set | recipients:
+ steven.daprano, eric.smith, docs@python, dmitriym |
2019-07-06 01:14:12 | steven.daprano | set | messageid: <1562375652.92.0.434565471498.issue37512@roundup.psfhosted.org> |
2019-07-06 01:14:12 | steven.daprano | link | issue37512 messages |
2019-07-06 01:14:12 | steven.daprano | create | |
|