Re: Generators vs. Functions?

2006-02-06 Thread Bengt Richter
On Sun, 05 Feb 2006 19:14:29 +1100, Steven D'Aprano <[EMAIL PROTECTED]> wrote: >On Sun, 05 Feb 2006 03:31:24 +, Neil Schemenauer wrote: > >> Peter Hansen <[EMAIL PROTECTED]> wrote: >>> More precisely, the state of the function is *saved* when a yield >>> occurs, so you certainly don't *recrea

Re: Generators vs. Functions?

2006-02-05 Thread Magnus Lycka
Duncan Booth 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. > > I get the same, but the difference is much less on my system: With Python 2.4? Doesn't surprise me a bit. I t

Re: Generators vs. Functions?

2006-02-05 Thread Steven D'Aprano
On Sun, 05 Feb 2006 16:14:54 +, Neil Schemenauer wrote: > Steven D'Aprano <[EMAIL PROTECTED]> wrote: >> Have you actually measured this, or are you just making a wild >> guess? > > I haven't timed it until now but my guess it not so wild. I'm > pretty familiar with the generator implementati

Re: Generators vs. Functions?

2006-02-05 Thread Neil Schemenauer
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > Have you actually measured this, or are you just making a wild > guess? I haven't timed it until now but my guess it not so wild. I'm pretty familiar with the generator implementation (having written the initial version of it). In Python 2.3, resuming

Re: Generators vs. Functions?

2006-02-05 Thread Steven D'Aprano
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 funct

Re: Generators vs. Functions?

2006-02-05 Thread Duncan Booth
Steven D'Aprano wrote: t1.timeit() > 0.63980388641357422 ... t2.timeit() > 0.82081794738769531 > > So on the basis of my tests, there is a small, but significant speed > advantage to _calling_ a function versus _resuming_ a generator. I get the same, but the difference is much less on

Re: Generators vs. Functions?

2006-02-05 Thread Fredrik Lundh
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. (hint: functions and generators do d

Re: Generators vs. Functions?

2006-02-05 Thread Steven D'Aprano
On Sun, 05 Feb 2006 03:31:24 +, Neil Schemenauer wrote: > Peter Hansen <[EMAIL PROTECTED]> wrote: >> More precisely, the state of the function is *saved* when a yield >> occurs, so you certainly don't *recreate* it from scratch, but merely >> restore the state, and this should definitely be

Re: Generators vs. Functions?

2006-02-04 Thread Neil Schemenauer
Peter Hansen <[EMAIL PROTECTED]> wrote: > More precisely, the state of the function is *saved* when a yield > occurs, so you certainly don't *recreate* it from scratch, but merely > restore the state, and this should definitely be faster than creating it > from scratch in the first place. Right

Re: Generators vs. Functions?

2006-02-04 Thread Peter Hansen
Joseph Garvin wrote: > Wolfgang Keller wrote: >>If this is actually also true in the general case, and not due to eventual >>non-representativeness of the test mentioned above, is it simply due to a >>less-than-optimum implementation of generators in the current Pyython >>interpreter and thus li

Re: Generators vs. Functions?

2006-02-04 Thread Max
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 callin

Re: Generators vs. Functions?

2006-02-04 Thread Joseph Garvin
Wolfgang Keller wrote: >If this is actually also true in the general case, and not due to eventual >non-representativeness of the test mentioned above, is it simply due to a >less-than-optimum implementation of generators in the current Pyython >interpreter and thus likely to change in the futu

Generators vs. Functions?

2006-02-04 Thread Wolfgang Keller
Hello, in <[EMAIL PROTECTED]>, Magnus Lycka <[EMAIL PROTECTED]> posts the result of a short test that seems to indicate that resuming a generator takes more time than calling a function. If this is actually also true in the general case, and not due to eventual non-representativeness of the te