Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.053 X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'explanation': 0.09; 'width': 0.09; 'def': 0.12; 'algorithm.': 0.16; 'effect.': 0.16; 'expensive,': 0.16; 'implemented,': 0.16; 'words.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'basically': 0.19; 'seems': 0.21; 'machine': 0.22; 'memory': 0.22; 'import': 0.22; 'print': 0.22; 'initial': 0.24; 'math': 0.24; 'paul': 0.24; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'writes:': 0.31; 'running': 0.33; 'guess': 0.33; 'subject:the': 0.34; 'problem': 0.35; 'skip:u 20': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'expensive': 0.39; 'to:addr:python.org': 0.39; 'skip:x 10': 0.40; 'lower': 0.61; 'more': 0.64; 'management': 0.65; 'natural': 0.68; 'obvious': 0.74; 'paper': 0.75; '2015': 0.84; 'multiplying': 0.84; 'predicts': 0.84; 'upwards': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Jo9ZO0Sl9Vpuy1oWAW+Un+o15EBsq+w7iDJ1puxQmKI=; b=tmpAgspvBrw8rgEpBmVMSv0RG3p0/X5h8VfTBGXMBQLKowm/h9QcoJzrrEI8XSPPYi kc0SYP9bh1UDkNK6cU/vxJ8wzZULFx+TWliv1ehiHSHZtEMRl4oiYbGOZYIbgF4hSD16 YRAbfz1JJ4A2YoETk/HrUT/EEIhuolaXRwq/RDeJZnCz1GQv41P91Jo8dtbVJOBydCZy yjpoQVr54p3z+PhF/vrrGjLrrFInbyW5UHPH4FacPks4jPaH9sImhtDjMHPRoRcINeRP 9A2ZiiYvDwcviT+G5r0jqYgCDfVPOw871QE4uZ4W12LUnKqq4nuTbxa073/JMdt+7QsL OeDg== X-Received: by 10.107.136.89 with SMTP id k86mr38785400iod.63.1430927296526; Wed, 06 May 2015 08:48:16 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <8761853fkd.fsf@jester.gateway.sonic.net> References: <87h9rvm576.fsf@Equus.decebal.nl> <55499304$0$12978$c3e8da3$5496439d@news.astraweb.com> <5549b41f$0$12927$c3e8da3$5496439d@news.astraweb.com> <8761853fkd.fsf@jester.gateway.sonic.net> From: Ian Kelly Date: Wed, 6 May 2015 09:47:36 -0600 Subject: Re: Throw the cat among the pigeons To: Python Content-Type: text/plain; charset=UTF-8 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430927606 news.xs4all.nl 2954 [2001:888:2000:d::a6]:36833 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90061 On Wed, May 6, 2015 at 9:12 AM, Paul Rubin wrote: > Steven D'Aprano writes: >> Multiplying upwards seems to be more expensive than multiplying >> downwards... I can only guess that it has something to do with the way >> multiplication is implemented, or perhaps the memory management >> involved, or something. Who the hell knows? > > It seems pretty natural if multiplication uses the obvious > quadratic-time pencil and paper algorithm. The cost of multiplying m*n > is basically w(m)*w(n) where w(x) is the width of x in machine words. > So for factorial where m is the counter and n is the running product, > w(m) is always 1 while w(n) is basically log2(n!). From > > from math import log > def xfac(seq): > cost = logfac = 0.0 > for i in seq: > logfac += log(i,2) > cost += logfac > return cost > > def upward(n): return xfac(xrange(1,n+1)) > def downward(n): return xfac(xrange(n,1,-1)) > > print upward(40000),downward(40000) > > I get: 10499542692.6 11652843833.5 > > A lower number for upward than downward. The difference isn't as large > as your timings, but I think it still gives some explanation of the > effect. That was my initial thought as well, but the problem is that this actually predicts the *opposite* of what is being reported: upward should be less expensive, not more.