Needless copying in iterations?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Sep 15 20:40:13 EDT 2007


On Sun, 16 Sep 2007 00:05:58 +0000, Marc 'BlackJack' Rintsch wrote:

> On Sat, 15 Sep 2007 14:58:15 -0700, James Stroud wrote:
> 
>> I was staring at a segment of code that looked like this today:
>> 
>>     for something in stuff[x:y]:
>>       whatever(something)
>> 
>> and was wondering if the compiler really made a copy of the slice from
>> stuff as the code seems to suggest, or does it find some way to produce
>> an iterator without the need to make a copy (if stuff is a built-in
>> sequence type)?
> 
> The compiler can't "optimize" this as it would change the semantics.
> There's no way for the compiler to tell if this copy really is
> "needless". `whatever()` may change `stuff[i]` where `i` is in `x:y` and
> this may lead to different results wether it iterates over a copy or the
> original.

In *general* the compiler can't tell, but in specific cases it could. A 
(hypothetical) optimizing compiler would tell the difference between:


for item in alist[1:5]:
    print item # no possible side-effects


for item in alist[1:5]:
    function(alist, item) # there might be side-effects


for item in alist[1:5]:
    alist.append(item) # side-effects DON'T matter


for i, item in enumerate(alist[1:5]):
    alist[i+1] = function(item) # side-effects DO matter


Of course this is all besides the point, since no such optimizing 
compiler exists today, at least not for CPython. Any volunteers?


-- 
Steven.



More information about the Python-list mailing list