Python quick question about comprehensions vs list comprehensions

John Doe :

Just wanted to ask why this(list comprehension, if I'm not mistaken):

def s(number):
    return sum([n for n in range(number) if n%3==0 or n%5==0])
s(100)

is twice as fast(108 steps on visualize python) as this(202 steps):

def s(number):
    return sum(n for n in range(number) if n%3==0 or n%5==0)
s(100)

?

And also, although the first code is faster, does the second code have any advantages in any cases? Maybe, uses less memory? Just spitballing, don''t really have any idea what I'm talking about. Any clarification would be greatly appreciated.

Tupteq :

Performance of both of your snippets is quite similar, apparently not every step is equal. For small values of number first code (list) is slightly faster, but for larger number second code (generator) wins.

Other thing is memory usage - creating a list requires amount of memory proportional to its size, so larger number consumes more RAM. Moreover, as list grows it requires memory reallocations, which eventually triggers garbage collector (timeit() by default disables gc, mangling results). On the other hand, generator version uses the same (minimal) amount of memory for any number.

The conclusion is that you should use generator expressions whenever possible. It's especially important when you care of memory footprint and/or you operate on large numbers. Also, this way your code is slightly shorter and cleaner (arguable).

This subject is explained in PEP 289, introducing generator expressions.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=5192&siteId=1