Another thing to note is that “uses a lot less memory” often means “faster.” The lazy (iterator) version consumes each product immediately after calculating it, adding it to the current amount. The product and current amount are almost certainly in the L1 cache. If you first calculate all the products, then depending on the number of elements that will be sure that the first calculated products will be knocked out of the L1 cache, and then from the L2 cache and ... so that at the second pass, finally add them together , all products have a low memory hierarchy (and, in extreme cases, should be read from the page file).
But I don’t understand what you mean by "don’t see how he behaves differently than". The final calculated result is the same anyway.
Tim peters
source share