Generators vs. Functions?

Max rabkin at mweb[DOT]co[DOT]za
Sat Feb 4 17:34:23 EST 2006


Joseph Garvin wrote:
> 
> I am not a CPython or PyPy hacker, but I would guess that it will always 
> be slower as a matter of principal. When resuming a generator you have 
> to resetup the state the function was in when it was last called, which 
> I think should always be more costly than calling the function with a 
> clean state.
> 
> Someone want to correct me?

In cases where there are thousands of (large) values to return, the list 
(as returned by the function) may be large enough to require memory 
paging, whereas the generator only returns one value at a time.

> 
> Whether or not the difference is that significant though I am unsure. It 
> may be small enough that for most applications no one cares.

I just wrote an application which retrieves values from a 300mb 
database, and got a significant speedup using iterators.

--Max



More information about the Python-list mailing list