Any way to speed up the Python long int bitwise operation?

I am writing a chess program in Python and I am using python-chess to represent the board and move the generation, etc. This is generally very good and has very useful features.

However, since it is in pure Python, it is now the bottleneck of my AI. Python's long-running integer and its bitwise operations are widely used in a module, for example

x = b & -b
b ^= x

if not x & 0xffffffff:
    x >>= 32
    r |= 32

Is there a way to speed up this kind of operation in Python, with some other module? Is it possible without rewriting in C or Fortran? I tried the numba package, but it doesn't seem to be able to compile python long int.

Many thanks.

+4
source share
2

gmpy2 , , Python.

:

In [3]: x=12345678901234567890
In [4]: %timeit y=x;y>>=32
10000000 loops, best of 3: 113 ns per loop
In [5]: x=gmpy2.mpz(x)
In [6]: %timeit y=x;y>>=32
10000000 loops, best of 3: 71.9 ns per loop

mpz Python, . gmpy2 , xmpz. .

In [9]: x=gmpy2.xmpz(0)
In [10]: bin(x)
Out[10]: '0b0'
In [11]: x[4]=1
In [12]: bin(x)
Out[12]: '0b10000'

xmpz, / .

+2
+1

Source: https://habr.com/ru/post/1568552/


All Articles