Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python quick question about comprehensions vs list comprehensions

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.

like image 827
John Doe Avatar asked Feb 14 '20 10:02

John Doe


People also ask

Are list comprehensions faster than loops?

Because of differences in how Python implements for loops and list comprehension, list comprehensions are almost always faster than for loops when performing operations.

Are list comprehensions faster than map?

Map function is faster than list comprehension when the formula is already defined as a function earlier. So, that map function is used without lambda expression.

Are list comprehensions faster than append?

The list comprehension is 50% faster. Why?

Why are list comprehensions faster in Python?

List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration.

Why is the Python list comprehension faster than the C list?

Even factoring out the time it takes to lookup and load the append function, the list comprehension is still faster because the list is created in C, rather than built up one item at a time in Python. Show activity on this post.

What is list comprehension?

1 Comprehension of the list is an effective means of describing and constructing lists based on current lists. 2 Generally, list comprehension is more lightweight and simpler than standard list formation functions and loops. 3 We should not write long codes for list comprehensions in order to ensure user-friendly code. More items...

What is the difference between list comprehensions and for loops?

[‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘ ‘, ‘4’, ‘ ‘, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘!’] The list comprehensions are more efficient both computationally and in terms of coding space and time than a for loop. Typically, they are written in a single line of code.

When to use comprehensions in Python?

Comprehensions in python can be used in all the ways above to create readable code but when we use nested comprehensions we need to be careful as it can cause the code to become less readable. A nested comprehension is a term we use to refer to comprehensions being done within other comprehensions.


1 Answers

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.

like image 78
Tupteq Avatar answered Oct 19 '22 20:10

Tupteq