1 min readAug 16, 2020
In your scenario 2, you instantiate the generator but you never use it.
This is why the time difference is so great. In Scenario 1 "return cubes" is called, and the entire list is walked. In scenario 2 you don't actually use "for val in getcubes(1000000)".
Just compare the "cubes" object in both scenarios, one is very large, the other is empty.