Challenge: optimizing isqrt

Dave Angel davea at davea.name
Sun Nov 23 16:43:09 CET 2014


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

>
>
>
> On Thu, Nov 20, 2014 at 6:00 PM, Serhiy Storchaka <storchaka at 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



More information about the Python-list mailing list