Performance of lists vs. list comprehensions

Arnaud Delobelle arnodel at googlemail.com
Tue Jan 19 16:55:49 EST 2010


Gerald Britton <gerald.britton at gmail.com> writes:

> That's surprising. I wouldn't implement it that way at all.  I'd use a
> dynamically-expanding buffer as I suggested.  That way you get a
> single pass and don't have to calculate anything before you begin.  In
> the best case, you'd use half the memory (the result just fits in the
> buffer after its last expansion and no saved tuple).  In the worst
> case, the memory use is about the same (you just expanded the buffer
> using a 2x expansion rule then you hit the last item).
>
> Still I suppose the author thought of that approach and rejected it
> for reasons I can't yet see.

I don't know the reasons, but I'm guessing they could be historic.
Before Python had iterators, str.join would mostly have been only given lists
and tuples as arguments, in which case the current approach seems to be
the most appropriate.  Later, when things like generator functions and
generator expressions were introduced, perhaps str.join wasn't optimized
to accomodate them.

-- 
Arnaud



More information about the Python-list mailing list