Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.tele2net.at!news.panservice.it!feed.xsnews.nl!border01.ams.xsnews.nl!feeder04.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F10-10.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> <87383fo062.fsf@Equus.decebal.nl> <87twvvmigr.fsf@Equus.decebal.nl> 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 13:12:18 +0200 Message-ID: <87lhh7mdwt.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:ExxZuRPk1okZp7aoNMqGboZukd0= MIME-Version: 1.0 Content-Type: text/plain Lines: 62 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1430565290 news.kpn.nl 21102 81.207.62.244@kpn/81.207.62.244:37157 Xref: csiph.com comp.lang.python:89765 Op Saturday 2 May 2015 12:35 CEST schreef Dave Angel: > On 05/02/2015 05:33 AM, Cecil Westerhof wrote: > > Please check your email settings. Your messages that you type seem > to be indented properly, but those that are quoting earlier messages > (even your own) are not. See below. I suspect there's some problem > with how your email program processes html messages. > >> Op Saturday 2 May 2015 10:26 CEST schreef Cecil Westerhof: >>> >>> 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) >> >> Stupid me 'x == z' should be 'z > x' >> > > I can't see how that is worth doing. The recursive version is > already a distortion of the definition of factorial that I learned. > And to force it to be recursive and also contort it so it does the > operations in the same order as the iterative version, just to gain > performance? > > If you want performance on factorial, write it iteratively, in as > straightforward a way as possible. Or just call the library > function. > > Recursion is a very useful tool in a developer's toolbox. But the > only reason I would use it for factorial is to provide a simple > demonstration to introduce the concept to a beginner. And that was what I was doing here: showing that tail recursion can have benefits. By the way I have seen factorial mostly implemented recursive. Also I am now testing with very large values. The version where I use both tail recursion versions are faster as the iterative version. Calculating 500.000: iterative: 137 seconds tail recursion optimised: 120 seconds tail recursion simple: 130 seconds You have to be careful that you do not fall into the trap that you are sure your way is the best way. I have had the same on the Scala list. I told there that for a certain function it was better to use an iterative version as a tail recursive function. They did not want to believe it at first. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof