Generator Comprehensions

Raymond Hettinger othello at
Tue Jan 29 13:38:33 EST 2002

"Just van Rossum" <just at> wrote in message
news:3C567251.3E8AAC9B at
> 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

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):
print time.time() - start

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

More information about the Python-list mailing list