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.
Because of differences in how Python implements for loops and list comprehension, list comprehensions are almost always faster than for loops when performing operations.
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.
The list comprehension is 50% faster. Why?
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.
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.
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...
[‘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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With