[Python-ideas] Create a StringBuilder class and use it everywhere
stefan_ml at behnel.de
Thu Aug 25 18:58:01 CEST 2011
k_bx, 25.08.2011 17:28:
> I don't mind using u''.join, but it just doesn't make people think about speed at all.
When I see something like a StringBuilder, I guess the first thing I'd
wonder about is why the programmer didn't just use StringIO() or even just
''.join(). That makes the code appear much more magic than it eventually
turns out to be when looking closer. Plus, it's doomed to be slower, simply
because it goes through more indirections.
You may be right that using StringIO won't make people think about speed.
Somebody who doesn't know it would likely go: "oh, that's nice - writing to
a string as if it were a file - I get that". So it tells you what it does,
instead of distracting you into thinking about performance implications.
That's the beauty of it.
Optimisations are just that: optimisations. They are orthogonal to what the
code does - or at least they should be. Even string concatenation can
preform just fine in many cases.
> The most popular (as from what I can see) thing right now where people start seeing
> that += is slow is when they try to do that on PyPy (which doesn't have hack like CPython,
> who is still slow) and ask "why my pypy code is sooooo slow".
Sounds like yet another reason not to do it then.
Seriously, there are hardly any language runtimes out there where continued
string concatenation is efficient, let alone guaranteed to be so. You just
shouldn't expect that it is. The optimisation in CPython was simply done
because it *can* be done, so that simple cases (and stupid benchmarks) can
continue to use simple concatenation and still be efficient. Well, in some
cases at least.
More information about the Python-ideas