Assignment saves time?

rpglover64 at rpglover64 at
Fri Feb 15 11:37:26 CET 2008

In timing my code, which I probably screwed up, I found something
given that li=range(1000),

the code block (enclosed in braces for clarification)
runs slightly faster than

I tested this using the timeit module, and though the difference was
small, I would have expected the first code block to do slightly
worse, because it has to assign the length to a variable and then call
the variable (still having to compute the length and having either one
or two additional operations to perform).

I would appreciate it if more knowledgeable coders could
a) point out why I'm wrong and show me how to test it correctly. or
b) explain why this occurs in terminology understandable to someone
who knows next to nothing about the internal workings of python.

I highly appreciate your any response I may receive.  Thank you in

More information about the Python-list mailing list