Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.pionier.net.pl!feed.xsnews.nl!border03.ams.xsnews.nl!feeder04.ams.xsnews.nl!feeder03.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F10-05.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Python is not bad ;-) Organization: Decebal Computing References: <87mw1q9jqw.fsf@Equus.decebal.nl> <87383hj4zj.fsf@elektro.pacujo.net> <87d22lx38x.fsf@Equus.decebal.nl> <55432557$0$12994$c3e8da3$5496439d@news.astraweb.com> X-Face: "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR,v+Pti8=Vi/Z"g^?b"E X-Homepage: http://www.decebal.nl/ Date: Sat, 02 May 2015 10:26:13 +0200 Message-ID: <87383fo062.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:K220dEjFrB2emYOUVcvKSUK47ng= MIME-Version: 1.0 Content-Type: text/plain Lines: 72 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1430555331 news.kpn.nl 21118 81.207.62.244@kpn/81.207.62.244:36302 Xref: csiph.com comp.lang.python:89747 Op Friday 1 May 2015 09:03 CEST schreef Steven D'Aprano: > On Thu, 30 Apr 2015 09:30 pm, Cecil Westerhof wrote: > >> Tail recursion would nice to have also. > > People coming from functional languages like Lisp and Haskell often > say that, but how many recursive algorithms naturally take a > tail-call form? Not that many. One example: def factorial(x, y = 1): return y if x == 1 else factorial(x - 1, x * y) def factorial_iterative(x): result = 1 for i in range(2, x + 1): result *= i return result Executing factorial(985) 100.000 times takes 54 seconds. While executing factorial_iterative(985) takes 34 seconds. Also you can not call factorial with a value that is much bigger because of recursion depth. You can call factorial_iterative with 1.000.000. I made also a version that simulates tail recursion: def factorial_tail_recursion(x): y = 1 while True: if x == 1: return y y *= x x -= 1 This is that a lot less efficient as the iterative version. It takes 43 seconds. But it is a lot better as the normal recursive version: about 25%. The iterative version is about 25% more efficient as the tail recursion version. With larger values it decreases. Calculating onetime for 5.000.000 takes 117 and 131 seconds. Just 10% faster. That is mostly because the tail recursion version starts multiplying at the high end. I wrote a second version: def factorial_tail_recursion2(x): y = 1 z = 1 while True: if x == z: return y y *= z z += 1 This has almost the performance of the iterative version: 34 and 121 seconds. So I made a new recursive version: def factorial_recursive(x, y = 1, z = 1): return y if x == z else factorial_recursive(x, x * y, z + 1) But this take almost the same tame as the other. Probably the recursive calls are more important as the multiplications. I find factorial a lot cleaner code as factorial_iterative, so here tail recursion would be beneficial. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof