Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; 'true,': 0.05; 'compiler': 0.07; 'explicit': 0.07; 'extracted': 0.09; 'implements': 0.09; 'url:github': 0.09; 'def': 0.12; '1):': 0.16; 'loops': 0.16; 'seconds.': 0.16; 'url:py': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'implementing': 0.19; 'version.': 0.19; 'code,': 0.22; 'header:User-Agent:1': 0.23; 'why.': 0.24; "i've": 0.25; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'code': 0.31; 'assert': 0.31; 'faster,': 0.31; 'so-called': 0.31; 'testing.': 0.31; 'framework': 0.33; 'skip:d 20': 0.34; 'subject:the': 0.34; 'but': 0.35; 'version': 0.36; 'functions.': 0.36; "didn't": 0.36; 'example,': 0.37; 'two': 0.37; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'took': 0.61; "you've": 0.63; 'myself': 0.63; 'more': 0.64; 'finally': 0.65; 'between': 0.67; 'determine': 0.67; 'received:74.208': 0.68; 'webpage': 0.68; 'difference.': 0.84; 'differences,': 0.84; 'iterative': 0.84; 'url:master': 0.84 Date: Tue, 05 May 2015 14:45:02 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Throw the cat among the pigeons References: <87h9rvm576.fsf@Equus.decebal.nl> <871tixlmp6.fsf@Equus.decebal.nl> <750db03b-e393-43ff-9ccf-5cc050af7324@googlegroups.com> <87zj5jf15q.fsf@Equus.decebal.nl> In-Reply-To: <87zj5jf15q.fsf@Equus.decebal.nl> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K0:TO6UL0tZgdevOAROzMS09MYKDdZMxtYL/G0vwqoKldC/ZK/L1bv 2RTl8fFrlo3vS+N0JOqv3Ge1faJ9nKXUdf8sdWqyOBBJXRGvtO9/k9hokjjXbLv55VHNwB/ I9xWUumvZJ4/tJu7B0e1D7Rwt6XtO671MJh94BOu7U/jtm21mofXTGrOAopBDcFjXSClhTn AHZ98Lb0ahfXAJIqVPAFg== X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430851516 news.xs4all.nl 2882 [2001:888:2000:d::a6]:39898 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89990 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 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. -- DaveA