3 ms·
It's troubling to read something about Python optimization/performance without mention that this is atrocious in a loop string = string + s That is such a cod
by njharman 15y ago
It's troubling to read something about Python optimization/performance without mention that this is atrocious in a loop
string = string + s
That is such a code smell I cringed every time I saw it.
Should append to list and join at the end (once). Which, I bet (I, or you, don't know anything until we actually profile), dumping the += is significant reason why last two examples where much faster.
- chubot 15y agoI still follow that rule, perhaps somewhat anachronistically. This has been long optimized in CPython so it's not quadratic: $ python -V Python 2.5.2 $ python -m timeit -s 'v=["a"]' 'v.append("a")' 1000000 loops, best of 3: 0.199 usec per loop $ python -m timeit -s 's="a"' 's+="a"' 10000000 loops, best of 3: 0.127 usec per loop The string append is actually FASTER now. Tried with strings of length 5 and it is still faster.