Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.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!feeder01.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F09-07.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Throw the cat among the pigeons Organization: Decebal Computing References: <87h9rvm576.fsf@Equus.decebal.nl> <871tixlmp6.fsf@Equus.decebal.nl> <750db03b-e393-43ff-9ccf-5cc050af7324@googlegroups.com> <87zj5jf15q.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: Tue, 05 May 2015 21:42:52 +0200 Message-ID: <87r3qug69v.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:RVvTZr9VsJ3gcpgJkSlTE6aXOWU= MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Lines: 79 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1430855088 news.kpn.nl 21065 81.207.62.244@kpn/81.207.62.244:38103 Xref: csiph.com comp.lang.python:89997 Op Tuesday 5 May 2015 20:45 CEST schreef Dave Angel: > On 05/05/2015 12:18 PM, Cecil Westerhof wrote: > >> >> 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. And that was with >> myself implementing the tail recursion. I expect the code to be >> more efficient when the compiler implements the tail recursion. >> > > > You've said that repeatedly, so I finally took a look at your > webpage > > https://github.com/CecilWesterhof/PythonLibrary/blob/master/mathDecebal.py > > I didn't have your framework to call the code, so I just extracted > some functions and did some testing. I definitely need to take care of documentation. It can be called with: python3 mathDecebal.py --factorial The problem is that it will do correctness and speed test. I have to split those in two different things. And use a different file for both. Maybe make a directory test and put a correctness_.py and a speed_.py. > I do see some differences, > where the so-called tail_recursive functions are sometimes faster, > but I did some investigating to try to determine why. > > > I came up with the conclusion that sometimes the multiply operation > takes longer than other times. And in particular, i can see more > variation between the two following loops than between your two > functions. > > > def factorial_iterative(x, simple=False): > assert x >= 0 > result = 1 > j=2 > if not simple: > for i in range(2, x + 1): > result *= i > j += 1 > else: > for i in range(2, x + 1): > result *= j > j += 1 > pass > > return result > > When the "simple" is True, the function takes noticeably and > consistently longer. For example, it might take 116 instead of 109 > seconds. For the same counts, your code took 111. > > I've looked at dis.dis(factorial_iterative), and can see no explicit > reason for the difference. I would say that a variable that is filled by a range is different as a normal variable. Do not ask me why. ;-) Even if you (general not personal you) think that the tail recursion is a waist of time, this is an interesting result I think. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof