Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'odd': 0.07; 'python3': 0.07; '101': 0.09; '22,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; '10000000': 0.16; 'ast': 0.16; 'exponent': 0.16; 'iterated': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'tried:': 0.16; 'accordingly.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'skip:1 30': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'skip:n 60': 0.24; 'looks': 0.24; 'header:X -Complaints-To:1': 0.27; 'skip:( 40': 0.30; 'program,': 0.31; '100000': 0.31; '>>>>': 0.31; 'clever': 0.31; 'constant': 0.31; "skip:' 40": 0.31; 'time:': 0.31; 'something': 0.35; 'introducing': 0.36; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'more': 0.64; '10000': 0.68; 'increase': 0.74; 'potentially': 0.81; 'parity': 0.84; 'defeat': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: (-1)**1000 Date: Wed, 22 Oct 2014 12:29:45 +0200 Organization: None References: <54476a77$0$21664$426a74cc@news.free.fr> <5447786d$0$12750$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bd9a9d.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413973806 news.xs4all.nl 2855 [2001:888:2000:d::a6]:34508 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79426 ast wrote: > > "Chris Angelico" a écrit dans le message de > news:mailman.15058.1413968065.18130.python-list@python.org... >> On Wed, Oct 22, 2014 at 7:27 PM, ast wrote: >>> If i am writing (-1)**1000 on a python program, will the >>> interpreter do (-1)*(-1)*...*(-1) or something clever ? >>> >>> In fact i have (-1)**N with N an integer potentially big. >> >> Exponentiation is far more efficient than the naive implementation of >> iterated multiplication. > > In the very particular case of (-1)**N, I belive that Python check > the odd or even parity of N and provides the result accordingly. > > I tried: >>>> (-1)**1000000000000000000000000000000000 > 1 >>>> (-1)**1000000000000000000000000000000001 > -1 > > and it is instantaneous Not instantaneous once you defeat the peephole optimizer by introducing a variable: $ python3 -m timeit '(-1)**10000000000000000000000000000000001' 10000000 loops, best of 3: 0.0356 usec per loop $ python3 -m timeit -s'a = 10000000000000000000000000000000001' '(-1)**a' 100000 loops, best of 3: 3.23 usec per loop When you increase the exponent you might discern a pattern: $ python3 -m timeit -s 'a = 10**10' '(-1)**a' 1000000 loops, best of 3: 1.42 usec per loop $ python3 -m timeit -s 'a = 10**100' '(-1)**a' 100000 loops, best of 3: 11.6 usec per loop $ python3 -m timeit -s 'a = 10**1000' '(-1)**a' 10000 loops, best of 3: 101 usec per loop $ python3 -m timeit -s 'a = 10**10000' '(-1)**a' 1000 loops, best of 3: 992 usec per loop That looks like log(a) while a parity check takes constant time: $ python3 -m timeit -s 'a = 10**10' 'a & 1' 10000000 loops, best of 3: 0.124 usec per loop $ python3 -m timeit -s 'a = 10**100' 'a & 1' 10000000 loops, best of 3: 0.124 usec per loop $ python3 -m timeit -s 'a = 10**1000' 'a & 1' 10000000 loops, best of 3: 0.122 usec per loop