Why generators take long time?

Arshpreet Singh arsh840 at gmail.com
Tue Jan 19 02:27:46 EST 2016


I was playing with Generators and found that using Generators time is bit more than list-comprehensions or I am doing it wrong?


Function with List comprehensions:

def sum_text(number_range):
    return sum([i*i for i in xrange(number_range)])

%timeit sum_text(100000000)
1 loops, best of 3: 14.8 s per loop

Using generator Expressions:

def sum_text(number_range):

    return sum((i*i for i in xrange(number_range))) 

%timeit sum_text(100000000)

1 loops, best of 3: 16.4 s per loop





More information about the Python-list mailing list