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


Groups > comp.lang.python > #81377

Re: Challenge: optimizing isqrt

Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.04; 'inspired': 0.05; 'root': 0.05; 'lines,': 0.07; 'calculating': 0.09; 'measure': 0.09; 'shifting': 0.09; 'python': 0.11; 'def': 0.12; '6:00': 0.16; 'interest,': 0.16; 'order)': 0.16; 'processors': 0.16; 'top-post': 0.16; 'true:': 0.16; 'valueerror': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; 'thu,': 0.19; '>>>': 0.22; 'programming': 0.22; 'preferred': 0.22; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'stephen': 0.24; 'tend': 0.24; 'looks': 0.24; "i've": 0.25; '(for': 0.26; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; 'moved': 0.30; 'code': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'probably': 0.32; 'actual': 0.34; 'operations': 0.35; 'but': 0.35; 'there': 0.35; 'doubt': 0.36; 'example,': 0.37; 'positive': 0.37; 'two': 0.37; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'matter': 0.61; "you're": 0.61; 'kind': 0.63; 'more': 0.64; 'situation': 0.65; 'here': 0.66; 'close': 0.67; '20,': 0.68; 'optimized': 0.68; 'received:74.208': 0.68; 'square': 0.74; 'ideas.': 0.84; 'remarks': 0.84; 'timing.': 0.84; 'imagine': 0.93
Date Sun, 23 Nov 2014 10:43:09 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Challenge: optimizing isqrt
References <54543787$0$13005$c3e8da3$5496439d@news.astraweb.com> <m4la9e$5hl$1@ger.gmane.org> <CAP=-cKWj5yeL34GG5ay4v-V9i6nuTgF3VvmC8=PDOYruwp8q5Q@mail.gmail.com>
In-Reply-To <CAP=-cKWj5yeL34GG5ay4v-V9i6nuTgF3VvmC8=PDOYruwp8q5Q@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:RjlwZWnMB/cKgi4uEbhxU5S/7kfxFjgEmPf1RdtKkcs y5MMsqBw0lAUY9Z1qwCY4m2uFOd5qK3gCoc/jqTAd7J/6Mojx8 2n+e+m2TAj+QGSswsS/nt5YPE9Pkw0eOWKU1sJyLO/jX7vgC3/ fkdJY0oUWF6TsLs5GUPadXtSyJHXNGk9ovzkJeNo7DTdnOvFeG USOrlw9N+2dHO5AwqXuGiad8e2Uo0AklgDKrHl6P72RYwZcARu tpkcL/hFmaiN5JdhxOm1TiShPGI3wjNjzkH0fsyCnbwAN2Bn/f dJE4KZZ49EL8Tx2Ubu7qgsVkIP55PcWrIk2O5HXmk9PfsrvJqa a7vFxMRRwRgMnUcg16yQ=
X-UI-Out-Filterresults notjunk:1;
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16190.1416757395.18130.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416757395 news.xs4all.nl 2906 [2001:888:2000:d::a6]:54533
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:81377

Show key headers only | View raw


On 11/21/2014 03:15 AM, Stephen Tucker wrote:

>
>
>
> On Thu, Nov 20, 2014 at 6:00 PM, Serhiy Storchaka <storchaka@gmail.com>
> wrote:
>
>> On 01.11.14 03:29, Steven D'Aprano wrote:
>>
>>> There is an algorithm for calculating the integer square root of any
>>> positive integer using only integer operations:
>>>
>>
>> Here is my optimized implementation inspired by Christian's ideas.
>>
>>  ....
>> def isqrt(n):
>>      if n < 0:
>>          raise ValueError
>>      if n == 0:
>>          return 0
>>      if n <= C1:
>>          return int(math.sqrt(n))
>>      if n <= C2:
>>          x = int(math.sqrt(n))
>>      else:
>>          b = (n.bit_length() - C3) // 2
>>          x = int(math.sqrt(n >> (2 * b))) << b
>>      y = (x + n // x) // 2
>>      if y == x:
>>          return x
>>      while True:
>>          x = y
>>          y = (x + n // x) // 2
>>          if y >= x:
>>              return x
>>
>> > Serhiy,
 >
 > This looks very good indeed. As a matter of interest, is there any
 > particular reason you have used 2*b instead of b+b? Might b+b be faster
 > than b*2?
 >
 > Also, in various lines, you use //2. Would >>1 be quicker? On reflection,
 > perhaps you have had to use //2 because >>1 cannot be used in those
 > situations.
 >
 > Stephen Tucker.
 >

(Please don't top-post here.  I moved your remarks after the code you're 
referencing, so that others may see it in the preferred order)

It's been probably two decades since I've been in a programming 
situation where that kind of difference mattered.  In Python in 
particular, the actual arithmetic or bit shifting is probably only one 
part in a hundred, and in modern processors all these operations tend to 
be very close in timing.

You're welcome to use the timeit module to try to measure it.  But I 
doubt it makes a difference of more than one part in a thousand.  And in 
some situations I can imagine b+b to be slower than 2*b. (For example, 
if b were global)


-- 
DaveA

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


Thread

Challenge: optimizing isqrt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-01 12:29 +1100
  Re: Challenge: optimizing isqrt Chris Angelico <rosuav@gmail.com> - 2014-11-01 13:12 +1100
  Re: Challenge: optimizing isqrt Christian Gollwitzer <auriocus@gmx.de> - 2014-11-01 09:02 +0100
    Re: Challenge: optimizing isqrt Christian Gollwitzer <auriocus@gmx.de> - 2014-11-01 09:09 +0100
    Re: Challenge: optimizing isqrt Chris Angelico <rosuav@gmail.com> - 2014-11-01 19:13 +1100
      Re: Challenge: optimizing isqrt Christian Gollwitzer <auriocus@gmx.de> - 2014-11-01 09:25 +0100
        Re: Challenge: optimizing isqrt Chris Angelico <rosuav@gmail.com> - 2014-11-01 19:33 +1100
          Re: Challenge: optimizing isqrt Christian Gollwitzer <auriocus@gmx.de> - 2014-11-01 09:38 +0100
            Re: Challenge: optimizing isqrt Chris Angelico <rosuav@gmail.com> - 2014-11-01 19:45 +1100
  Re: Challenge: optimizing isqrt Akira Li <4kir4.1i@gmail.com> - 2014-11-01 12:12 +0300
  Re: Challenge: optimizing isqrt Serhiy Storchaka <storchaka@gmail.com> - 2014-11-20 20:00 +0200
  Re: Challenge: optimizing isqrt Stephen Tucker <stephen_tucker@sil.org> - 2014-11-21 08:15 +0000
  Re: Challenge: optimizing isqrt Dave Angel <davea@davea.name> - 2014-11-23 10:43 -0500
  Re: Challenge: optimizing isqrt Serhiy Storchaka <storchaka@gmail.com> - 2014-11-25 21:31 +0200
  Re: Challenge: optimizing isqrt Dave Angel <davea@davea.name> - 2014-11-25 19:46 -0500
  Re: Challenge: optimizing isqrt Stephen Tucker <stephen_tucker@sil.org> - 2014-11-26 07:57 +0000
    Re: Challenge: optimizing isqrt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-26 21:25 +1100
  Re: Challenge: optimizing isqrt Serhiy Storchaka <storchaka@gmail.com> - 2014-11-26 18:24 +0200

csiph-web