Iterate from 2nd element of a huge list

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Feb 2 02:38:53 EST 2012


On Thu, 02 Feb 2012 07:23:04 +0000, Paulo da Silva wrote:

> Em 01-02-2012 04:55, Cameron Simpson escreveu:
>> On 01Feb2012 03:34, Paulo da Silva <p_s_d_a_s_i_l_v_a at netcabo.pt>
>> wrote:
> 
>> | BTW, iter seems faster than iterating thru mylist[1:]!
>> 
>> I would hope the difference can be attributed to the cost of copying
>> mylist[1:].
> I don't think so. I tried several times and the differences were almost
> always consistent.

Yes, actually iterating over a list-iterator appears to be trivially 
faster (although this may not apply to arbitrary iterators):

steve at runes:~$ python -m timeit -s "L=range(10000)" "for x in L: pass"
1000 loops, best of 3: 280 usec per loop
steve at runes:~$ python -m timeit -s "L=range(10000)" "for x in iter(L): 
pass"
1000 loops, best of 3: 274 usec per loop


The difference of 6 microseconds would be lost in the noise if the loops 
actually did something useful.

Also keep in mind that for tiny lists, the overhead of creating the 
iterator is probably much greater than the time of iterating over the 
list:

steve at runes:~$ python -m timeit -s "L=range(3)" "for x in L: pass"
1000000 loops, best of 3: 0.238 usec per loop
steve at runes:~$ python -m timeit -s "L=range(3)" "for x in iter(L): pass"
1000000 loops, best of 3: 0.393 usec per loop

But of course the difference is only relatively significant, in absolute 
terms nobody is going to notice an extra 0.1 or 0.2 microseconds.



-- 
Steven



More information about the Python-list mailing list