When Python outruns C++ ...

Peter Hansen peter at engcorp.com
Tue Apr 1 14:04:30 EST 2003


Roy Smith wrote:
> 
> For example, let's say you're building up a list and at the end, you
> want to know how many elements you added to the list.  The obvious
> thing to do would be to keep track of how many elements you add as you
> build the list.  [...]
> 
> I just tried it in Python, and (not surprisingly), found that it's not
> true in Python either.  [...]

What you're saying is that it's not true that redundant code in Python 
runs faster than code that skips the redundant part...  I don't find
that surprising either.

Put another way, and as you found out after your experiment, one
should expect a language like Python, or really any decent collection 
object, to keep track of how many items it contains without needing to
do an expensive scan of the entire collection each time the length is
asked for...

-Peter




More information about the Python-list mailing list