Generators vs. Functions?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Feb 5 06:08:02 EST 2006


On Sun, 05 Feb 2006 09:49:21 +0100, Fredrik Lundh wrote:

> Steven D'Aprano wrote:
> 
>> So on the basis of my tests, there is a small, but significant speed
>> advantage to _calling_ a function versus _resuming_ a generator.
> 
> now add state handling to your micro-benchmark, and see if the function
> example still runs faster.

¿Que Mr Fawlty?

Sorry, I'm not sure I follow what you mean. Do you mean, "Make the
function and generator do something significant"?

I expected that people would understand that I was talking only about the
overhead of calling the function or generator, not the overall time needed
to perform some useful task. Sorry for the less than clear explanation.


> (hint: functions and generators do different things, and are designed
> for different use cases.  they're not two different ways to do the same
> thing, and benchmarks that ignore that simple fact are pretty much use-
> less, except, perhaps, for a very small group of VM developers.)

I never meant to imply that generators were somehow "worse" than
functions. As you say, they have different purposes, and for the sort of
use case that generators are good for, the tiny extra overhead in
restoring a generator is a cost well worth paying.

But it is a cost. I personally don't believe it is a significant cost,
except maybe for the odd special case or two. 


-- 
Steven.




More information about the Python-list mailing list