Help- Recursion v. Iter Speed comparison

Robert Kern rkern at ucsd.edu
Thu Mar 3 23:49:51 CET 2005


actuary77 wrote:
> Robert Kern wrote:
> 
>> actuary77 wrote:
>>
>>> #================================================
>>> #  non-generator
>>> #================================================
>>>
>>>
>>> def f1(afunc,aseed,n):
>>>     values = [afunc(aseed)]
>>>     for i in range(n-1):
>>>         values.append(afunc(values[-1]))
>>>     return values[-1]
>>>
>>> _b=time()
>>> for _i in range(0,100):
>>>     _y = f1(myfunc,seed,n)
>>
>>
>>
>> Why do you do this? The whole point of this approach was to keep the 
>> intermediate results instead of recomputing them every time!
>>
> The point is to get some meaningful results when "timing" the function.

No, they're not meaningful. You're taking an intrinsically linear 
algorithm and making it quadratic. That's never a good thing. The 
function I gave you does exactly the same thing as the function you have 
here plus the loop, but it does it faster. You asked for faster ways of 
doing the task; I gave you one.

>> And why do you prepend underscores everywhere? It's bad Python style.
> 
> 
> I prepend underscore to differentiate local,private variables vs. global 
> variables.  Do not want to create a conflict.  It is my understanding 
> that this is the Python style for local v. global variables.

It isn't.

Just use non-obfuscated variable names. There are cases where you want a 
leading underscore, but you haven't hit one, yet.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list