Throw the cat among the pigeons
Terry Reedy
tjreedy at udel.edu
Tue May 5 19:22:55 EDT 2015
On 5/5/2015 5:12 PM, Cecil Westerhof wrote:
> Op Tuesday 5 May 2015 22:46 CEST schreef Terry Reedy:
>
>>> Well, I did not write many tail recursive functions. But what
>>> surprised me was that for large values the ‘tail recursive’ version
>>> was more efficient as the iterative version.
>>
>> In your first thread, what you mislabelled 'tail recursive version'
>> was an iterative while loop version
>
> That is because Python has no tail recursion,
Yes is does. Please stop using 'tail recursion' to refer to the absence
of recursion or the process of removing recursion. I wrote a tail
recursive function, and I believe you did too.
What Python does not have is automatic tail call optimization, or
specifically, tail recursion *elimination*. Both possibilities, the
general and specific, have been considered and rejected for excellent
reasons. I hinted at some of them in my post.
See https://en.wikipedia.org/wiki/Tail_call
for 'tail recursion' and 'tail call elimination'
What I believe you showed is that a while loop can be faster than a more
or less equivalent for loop that produces the same result by a slightly
different method. That is not surprising. Relative timings for CPython
vary a few percent between different combinations of Python version, C
compiler and settings, operating system, and cpu and other hardware.
--
Terry Jan Reedy
More information about the Python-list
mailing list