Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79426

Re: (-1)**1000

From Peter Otten <__peter__@web.de>
Subject Re: (-1)**1000
Date 2014-10-22 12:29 +0200
Organization None
References <54476a77$0$21664$426a74cc@news.free.fr> <mailman.15058.1413968065.18130.python-list@python.org> <5447786d$0$12750$426a74cc@news.free.fr>
Newsgroups comp.lang.python
Message-ID <mailman.15063.1413973806.18130.python-list@python.org> (permalink)

Show all headers | View raw


ast wrote:

> 
> "Chris Angelico" <rosuav@gmail.com> 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 <nomail@invalid.com> 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

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

(-1)**1000 "ast" <nomail@invalid.com> - 2014-10-22 10:27 +0200
  Re: (-1)**1000 Chris Angelico <rosuav@gmail.com> - 2014-10-22 19:54 +1100
    Re: (-1)**1000 "ast" <nomail@invalid.com> - 2014-10-22 11:27 +0200
      Re: (-1)**1000 Peter Otten <__peter__@web.de> - 2014-10-22 12:29 +0200
      Re: (-1)**1000 Michiel Overtoom <motoom@xs4all.nl> - 2014-10-22 12:38 +0200
      Re: (-1)**1000 Tim Chase <python.list@tim.thechases.com> - 2014-10-22 05:43 -0500
      Re: (-1)**1000 Peter Otten <__peter__@web.de> - 2014-10-22 13:02 +0200
      Re: (-1)**1000 Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-22 08:23 -0600
      Re: (-1)**1000 Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-22 08:29 -0600
      Re: (-1)**1000 Ned Batchelder <ned@nedbatchelder.com> - 2014-10-22 11:01 -0400
    Re: (-1)**1000 wxjmfauth@gmail.com - 2014-10-22 04:35 -0700
  Re: (-1)**1000 Peter Otten <__peter__@web.de> - 2014-10-22 11:13 +0200
  Re: (-1)**1000 Terry Reedy <tjreedy@udel.edu> - 2014-10-22 13:15 -0400
  Re: (-1)**1000 Dave Angel <davea@davea.name> - 2014-10-24 13:57 -0400
  Re: (-1)**1000 Tony the Tiger <tony@tiger.invalid> - 2014-10-26 20:07 +0000
    Re: (-1)**1000 Ned Batchelder <ned@nedbatchelder.com> - 2014-10-26 16:45 -0400
    Re: (-1)**1000 Stefan Behnel <stefan_ml@behnel.de> - 2014-10-29 20:31 +0100

csiph-web