简体   繁体   中英

Why would iterating over a list be much slower than iterating over an iterator in python?

I am wondering why would iterating over a list be much slower than iterating over an iterator in Python 2.7. Below is an example code and the output.

import timeit
stmt = """
    for i in range(1000000):
        pass
    """
print "================for loop with list=================="
t = timeit.Timer(stmt)
print min(t.repeat(3, 100))

print "================for loop with iterator=================="
stmt = """
    for i in seq:
        pass
    """
t = timeit.Timer(stmt, setup = "seq = iter(range(1000000))")
print min(t.repeat(3, 100))

Output:

================for loop with list==================
2.61899293756
================for loop with iterator==================
0.0191696885382

It seems iterating over an iterator is more than 10 times faster than over a list.

Your iterator test exhausts the iterator on the first run. All further runs end the loop immediately, which is quick. If it weren't for the fact that the setup code is reexecuted on each of the 3 timeit calls repeat makes, the iterator timing would be even faster.

If we regenerate the iterator on each run:

>>> timeit.timeit('for i in seq: pass', 'seq=range(1000000)', number=100)
2.4989398827775986
>>> timeit.timeit('for i in iter(seq): pass', 'seq=range(1000000)', number=100)
2.543197477789299

the difference vanishes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM