Flattening lists

rdmurray at bitdance.com rdmurray at bitdance.com
Thu Feb 5 12:29:46 EST 2009


Quoth rdmurray at bitdance.com:
> This is all premature optimization, except for the goopy code, which is
> presumably used enough to make it worth optimizing.  And guess what?
> The goopy code wins.  What the people theorizing about the speed of
> extend vs list creation miss is that the things with high overhead in the
> above functions are (1) isinstance and (2) the recursive function call.
> The goopy code avoids this by using type and is, and by unrolling the
> lowest level without a function call.  On the other hand, extend
> _is_ faster than append if you aren't creating a new list, so the
> goopy code can be optimized a little more.
> 
> I remembered the bit about high function call overhead, but the rest
> of it measured:

Oooh, that's embarrassing.  Not only didn't I read the code carefully
enough, I didn't test the actual output of the functions.  The goopy
code doesn't flatten to arbitrary depth, so of course it is faster.

--RDM




More information about the Python-list mailing list