It is true in many many cases. For a very simple illustration consider
def f():
x = list(range(10000))
yield from x
vs
list(range(10000))
The former includes the overhead of memory for both the materialized list and function execution frames.
The same thing happens when people naively split up generators that hold onto a lot of data, for example (this comes from real life experience where someone wanted to essentially “memoize with generators” a membership check on the response from a database call).
def check_expensive_in():
s = large_db_call()
while True:
x = yield
yield x in s
def expensive_filter():
f = check_expensive_in()
next(f)
def helper(item):
v = f.send(item)
f.next()
return v
while True:
items = yield
for item in items:
yield helper(item)
yield None
e = expensive_filter()
next(e)
e.send(some_list)
# iterate e until None.
(Sorry for any typos or minor glitches with send(), I am writing this on my phone as I eat breakfast.)
It’s a very simple issue, which is that memoizing with generators maintains the memory footprint of the memoized data _and_ additional memory footprint of the generator (and also of large sent values into the generator too, but this is less common).
It’s better to just materialize the things you need in memory and reuse them in regular function calls, which don’t have fixed permanent overhead for a long lifetime like generators.
This can absolutely happen with small data examples too, where generators are less efficient than just materializing everything, but normally nobody cares because with small data, the effect of any inefficiency won’t be noticed.
Even in that case though, generators often lead to spaghetti code like my example above, because depending on laziness as you compose multiple functions is just a poor conceptual way to organize code. Very rarely, but sometimes, it’s worth it to avoid memory bottlenecks or to do stream processing. But it’s overstated how often this matters generally - it’s very rare unless you’re in a specialized domain where that’s all do.
Lastly I’d like to say that your tone comes across as needlessly antagonistic and it seems extremely obvious you are engaging in bad faith. You don’t seem open to consider what I am saying, rather in a rush to demand some kind of “proof” with no willingness to think through it, and likely not seeking proof to learn anything but just to try to create shallow, undermining retorts.
I won’t be continuing to check back here or engage any further with you. If you want the last word in the thread, take it.