Generator Comprehensions

Raymond Hettinger othello at javanet.com
Tue Jan 29 19:38:33 CET 2002


"Just van Rossum" <just at xs4all.nl> wrote in message
news:3C567251.3E8AAC9B at xs4all.nl...
> I wonder (and am too lazy to actually try ;-) whether it matters much
> speed-wise if list comprehensions would be implemented with generators,
> ie. a list comp spelled like this:
>
>   def something(file):
>       sizes = [(len(line), line) for line in file.readline()]
>
> would be implemented like this:
>
>   def something(file):
>       def __temp_generator__():
>           for line in file.readline():
>               yield len(line), line
>       sizes = list(__temp_generator__())
>
> Now, if the list comprehension would start with the word "yield" it could
> simply mean the list() call should be omitted...
>
> Just

Great idea!

I tried out the timing of your implementation and found that it
makes makes list comprehensions only 4% slower.  In my test code,
the first version ran in 52.34 seconds and the second version took 54.60
seconds.



Raymond Hettinger


'Test speed of implementing list comprehensions with generators'
from __future__ import generators
import time

def t1(size):
    a = [ str(x) for x in xrange(size) if x&1 ]
    assert len(a) == size/2

def t2(size):
    def __temp_generator__():
        for x in xrange(size):
            if x&1:
                yield str(x)
    a = list( __temp_generator__() )
    assert len(a) == size/2

start = time.time()
for i in xrange(100):
    t1(100000)
print time.time() - start

start = time.time()
for i in xrange(100):
    t2(100000)
print time.time() - start






More information about the Python-list mailing list