Help- Recursion v. Iter Speed comparison

actuary77 actuary77 at elemental-systems.com
Wed Mar 2 01:28:08 EST 2005


Heiko Wundram wrote:
> On Wednesday 02 March 2005 06:03, actuary77 wrote:
> 
>>It now makes sense if I write it, (simple):
>>
>>def rec2(n):
>>     if n == 0:
>>         return []
>>     else:
>>         return [n] + rec2(n-1)
> 
> 
> Or, if you're not interested in a recursive function to do this job (which 
> should be way faster...):
> 
> 
>>>>def iter1(n):
> 
> ...     nl = range(1,n+1)
> ...     nl.reverse()
> ...     return nl
> ...
> 
>>>>print iter1(4)
> 
> [4, 3, 2, 1]
> 
> 
Recurse from 9999 time: 4.3059999942779541 seconds

Iter from 9999 time: 0.0099999904632568359 seconds

No comparison, why is recursion so slow?


Would using a generator be faster than recursion?
I really have complex list comprehension.


I am interating n times.
I have initial function: func(x)
I have initial seed value:  _aseed


iteration   value
   1        func(_aseed)
   2        func(func(_aseed))
  ....
   n       func(func(func...........(_aseed))


What would be the fastest way to do this?



More information about the Python-list mailing list