Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'true,': 0.05; 'explicit': 0.07; 'function,': 0.09; 'logic': 0.09; 'true)': 0.09; 'python': 0.11; 'def': 0.12; '1):': 0.16; 'arg': 0.16; 'arg):': 0.16; 'arg,': 0.16; 'loop.': 0.16; 'opposite': 0.16; 'seconds.': 0.16; 'time.time()': 0.16; 'with?': 0.16; 'wrote:': 0.18; 'looked': 0.18; '(but': 0.19; 'issue.': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'skip:{ 20': 0.24; 'versions': 0.24; "i've": 0.25; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'appear': 0.29; 'testing': 0.29; 'originally': 0.30; 'code': 0.31; 'that.': 0.31; 'assert': 0.31; 'this.': 0.32; 'quite': 0.32; 'linux': 0.33; 'skip:# 10': 0.33; 'actual': 0.34; 'maybe': 0.34; 'skip:d 20': 0.34; 'subject:the': 0.34; "can't": 0.35; 'test': 0.35; 'version': 0.36; 'functions.': 0.36; "i'll": 0.36; 'example,': 0.37; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'dave': 0.60; 'ian': 0.60; 'took': 0.61; 'simple': 0.61; 'first': 0.61; 'charset:windows-1252': 0.65; 'optimized': 0.68; 'received:74.208': 0.68; '2014,': 0.84; '2015': 0.84; 'difference.': 0.84; 'quicker': 0.84; 'replicate': 0.84; 'angel': 0.91 Date: Tue, 05 May 2015 17:00:05 -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> <55490FAE.8070501@davea.name> In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:lb7eGM6daG4eudTuOGL8N2ta73d3aYf8nomnUp0qHZY180bpMZU lWkbbxJx3WzeeWfgJeEIqRV+6dVTteWrNis8VGl7MDcnCGygjQSO2wqZQNJ1D22kJYfUoTN Qk+u7nZkfezG3OEzOiqmIfphTXmY2i7XTOTJHSuK5vtEYj79xDtje2OlC0JA37HOZJKJ+m7 vc9k7adJ7+8xgiuAACHXg== 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: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430859619 news.xs4all.nl 2905 [2001:888:2000:d::a6]:56059 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90001 On 05/05/2015 04:30 PM, Ian Kelly wrote: > On Tue, May 5, 2015 at 12:45 PM, Dave Angel wrote: >> 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 can't replicate this. What version of Python is it, and what value > of x are you testing with? > >> I've looked at dis.dis(factorial_iterative), and can see no explicit reason >> for the difference. > > My first thought is that maybe it's a result of the branch. Have you > tried swapping the branches, or reimplementing as separate functions > and comparing? > Logic is quite simple: def factorial_iterative(x, simple=False): assert x >= 0 result = 1 j=2 if not simple: for i in range(2, x + 1): #print("range value is of type", type(i), "and value", i) #print("ordinary value is of type", type(j), "and value", j) result *= i j += 1 else: for i in range(2, x + 1): result *= j j += 1 return result def loop(func, funcname, arg): start = time.time() for i in range(repeats): func(arg, True) print("{0}({1}) took {2:7.4}".format(funcname, arg, time.time()-start)) start = time.time() for i in range(repeats): func(arg) print("{0}({1}) took {2:7.4}".format(funcname, arg, time.time()-start)) repeats = 1 and arg is 10**4 loop(factorial_iterative, "factorial_iterative ", arg) My actual program does the same thing with other versions of the function, including Cecil's factorial_tail_recursion, and my optimized version of that. Python 3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2] on linux factorial_iterative (100000) took 3.807 factorial_iterative (100000) took 3.664 factorial_iterative (200000) took 17.07 factorial_iterative (200000) took 15.3 factorial_iterative (300000) took 38.93 factorial_iterative (300000) took 36.01 Note that I test them in the opposite order of where they appear in the function. That's because I was originally using the simple flag to test an empty loop. The empty loop is much quicker either way, so it's not the issue. (But if it were, the for-range version is much quicker). I think I'll take your last suggestion and write separate functions. -- DaveA