For Loop Faster In Counting Values

Asked By 0 points N/A Posted on -
qa-featured

It’s almost a factor two faster than the sum. And since the integrated sum should be about five times faster than manually adding a list (depending on the answer), it means that it is actually ten times faster! Does saving the fact of having to add only one counter for half the values, because the other half is ignored, is enough to take into account this difference?

SHARE
Answered By 0 points N/A #318462

For Loop Faster In Counting Values

qa-featured

sum is fast enough, but sum is not the cause of the slowdown. Three main factors contribute to the slowdown: Using a generator expression causes an overload to constantly stop and continue the generator. The version of your generator adds unconditionally, rather than just when the digit is even. This is more expensive if the figure is odd. Adding Boolean values ​​instead of Ints prevents the sum from using its fast integer path.

Generators offer two main advantages over list comprehension: they require much less memory and can be terminated prematurely if not all elements are needed. They offer no temporal benefit when all the elements are needed. The stop and the punctual recovery of a generator by element are quite expensive. If we replace genexp with an understanding list:

Related Questions