Skip to main content

A Cython-based chess library that optimizes the python-chess library

Project description

About This Project

This project is a heavily optimized Cython version of python-chess. It implements the same move generation functionality but with performance improvements using Cython and C++. Since python-chess is licensed under GPL v3, this project is also licensed under GPL v3.

Cython is a programming language that serves as a bridge between Python and C/C++. It allows you to write Python-like code that can be compiled into highly efficient C/C++ code. This makes it an ideal tool when you want to optimize the performance of certain Python programs while still leveraging the simplicity of Python. Cython also allows you to invoke custom C++ code to further boost speed by bypassing Python alltogether. In this project, I rewrote functions pertaining to move generation from the python-chess using Cython and C++. I use Cython to interact with Python objects from python-chess such as chess.Board or chess.Move objects but try to seperate code that involves bitmask parsing and other such operations into a C++ file for even more speed.

I may in the future look to optimize other portions of the python-chess library but as it stands, the overhead of calling Cython functions for small functions such as is_capture seem to be more costly than simply calling the same functions from python-chess. Move generation however is somewhat lengthy and therefore the Cythonized code (as well as the C++ injections) can give around a 40% improvement in move generation.

Note: This project includes C++ components and therefore requires the ability to compile C++ as well as Python. When pip installing the package, the required Python components come with it but you need to ensure you have the ability to compile the C++ files. To do this ensure you follow the build requirements below.

Build Requirements

Ensure you have Python 3.8 or greater installed on your system along with an udpated version of pip. When installing this package, all required Python dependencies will be installed alongside it. To ensure full functionality, confirm that you have a C++ compiler installed. If not, use the following instructions to download one.

Windows

  • Download and install Microsoft Visual C++ Build Tools 2022 or later from:
    https://visualstudio.microsoft.com/visual-cpp-build-tools/
  • Ensure you select the C++ CMake tools for Windows and MSVC compiler during installation.
  • To verify you have correctly installed the C++ compiler run this in the Developer Command Prompt (not the regular command prompt):
    cl
    
  • Note that simply installing MSVC build tools may not initialize your system properly. You may have to set environment variables or update your path with the cl.exe compiler.

Linux

  • Install the necessary C++ compiler and build tools:
    • For Ubuntu:
    sudo apt update && sudo apt install build-essential
    
    • For Fedora:
    sudo dnf install gcc-c++ make
    
  • To verify you have correctly installed the C++ compiler run this in a terminal:
    g++ --version 
    

For MacOS

  • Install Apple's command-line developer tools (includes clang for C++):

    xcode-select --install
    
  • To verify you have correctly installed the C++ compiler run this in a terminal:

    g++ --version 
    

Download

To download this package, use the following instructions:

  • For Linux:
    pip install cython-chess
    
  • for Windows (Not yet published on PYPI):
    pip install git+https://github.com/Ranuja01/cython-chess.git
    

Example Usage:

To use the module, simply import cython-chess as seen below:

import cython_chess

The module will self initialize, allowing you to use it automatically.

Since this module currently only improves the move generation speed, it still relies on the board objects from the original python-chess library. The following example demonstrates how to use the python-chess board object for move generation while utilizing the optimized cython-chess library to speed up the process. The start and end squares for move generation are defined using bitmasks from the original python-chess library:

import cython_chess
import chess

board = chess.Board()
cython_chess.generate_legal_moves(board,chess.BB_ALL,chess.BB_ALL)

The following code can be found in the example usage file. Run this script to test the speed improvements. Note: You may need to pip install timeit if not already done so for this

from timeit import default_timer as timer
import cython_chess
import chess

board = chess.Board()

## SPEED COMPARISON
t0= timer()
for i in range (100000):
    for move in board.generate_legal_moves():
        pass
t1 = timer()
print("Time elapsed: ", t1 - t0)

t0= timer()
for i in range (100000):
    for move in cython_chess.generate_legal_moves(board,chess.BB_ALL,chess.BB_ALL):
        pass
t1 = timer()
print("Time elapsed: ", t1 - t0)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cython_chess-0.3.tar.gz (111.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cython_chess-0.3-cp312-cp312-musllinux_1_2_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

cython_chess-0.3-cp312-cp312-musllinux_1_2_i686.whl (1.8 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ i686

cython_chess-0.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (637.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

cython_chess-0.3-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (617.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ i686

cython_chess-0.3-cp311-cp311-musllinux_1_2_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

cython_chess-0.3-cp311-cp311-musllinux_1_2_i686.whl (1.8 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ i686

cython_chess-0.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (635.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

cython_chess-0.3-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (624.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

cython_chess-0.3-cp310-cp310-musllinux_1_2_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

cython_chess-0.3-cp310-cp310-musllinux_1_2_i686.whl (1.8 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ i686

cython_chess-0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (616.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

cython_chess-0.3-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (610.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ i686

cython_chess-0.3-cp39-cp39-musllinux_1_2_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

cython_chess-0.3-cp39-cp39-musllinux_1_2_i686.whl (1.8 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ i686

cython_chess-0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (616.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

cython_chess-0.3-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (608.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ i686

cython_chess-0.3-cp38-cp38-musllinux_1_2_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ x86-64

cython_chess-0.3-cp38-cp38-musllinux_1_2_i686.whl (1.8 MB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ i686

cython_chess-0.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (628.3 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

cython_chess-0.3-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (621.3 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ i686

File details

Details for the file cython_chess-0.3.tar.gz.

File metadata

  • Download URL: cython_chess-0.3.tar.gz
  • Upload date:
  • Size: 111.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for cython_chess-0.3.tar.gz
Algorithm Hash digest
SHA256 5e2343cb7664f173ca19c9aba6d21164517cfc42c24f8f4cdfcf91a9d936a44b
MD5 685fec0255cd6d8c338e61be799fe6e1
BLAKE2b-256 87b17d88f46bc87d426daab41c36fa1c41a88f629d7905ab65e46855df2d6232

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 57e9edfe5ef81728d8bbe6d56def326f1166ee0245e1b71d3ad86f0a1555a43a
MD5 27528a65b4427221991e111d35b87ecf
BLAKE2b-256 38281ebc436fb81eb439a300c0026c68da12f575cf9bf22067750df69f771e76

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp312-cp312-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp312-cp312-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 1763f51540e6741b9ed5daba600c2dd2ccd7a7d4280d50a316218097e14e9dc0
MD5 80ad985a846f7e167a7b6db83a8110ff
BLAKE2b-256 ef83c3c67c16be2358b18df461dacf3754af641048748f7a3d5cf81866316eff

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1cb49a057d5331a7d3eb4677c994476c960ffe8400c7839da697b9008d7710e5
MD5 0fe4702794946fc036a49bd5e15cbb22
BLAKE2b-256 b129c3eb99db1a02b71390e6cfc04c8092fd2ced2125bce51566443613932265

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 1e1bef055229c1291453264b942214a8b23d3faa1e218307b312e4809a04d173
MD5 f8c85295c2db82fe029ebd7e3aa4d57a
BLAKE2b-256 a23539e119323f435f8999067c7729f48907b7db6e7dfb8ec3dbcbf32a15ba2f

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 741c7e5dabb7f9107133f44dc46414de90ecde0f5384daa630e194071e9bdaa9
MD5 66ba3f6a6ad7ed6ad5251ce74bc4fc78
BLAKE2b-256 8f814c4e7f16065dab69b8f744cedb80484d1b9dfd93744a3df60da28ef8a961

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp311-cp311-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp311-cp311-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 0d9f428c1b9c1de14c7fa56b4de27f3994707f5434249e60dd343c505389d65c
MD5 ddfe91d025b7f26de3c582321e88894e
BLAKE2b-256 cc599f0475248808a1d167114f7758b08d5d53a92cd9abe2fae6d27d44b12afb

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c82288df95e1c9fb4d58e2f7a294daf12c0a7bd5f3e525d7bdab811f87692617
MD5 4cb60a7310b571430358f331fb3dea42
BLAKE2b-256 fcc47d1adc3507620d6332bef9f9e4d74b3b2fc2b5dc549a0c3b9765c23889c3

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 2eebb652850e8d54e56d2d900253f1d6a3da62bed857a1489e4a845c1598c7f3
MD5 3f0972b43084bf5a973071646cb52975
BLAKE2b-256 ad3eb96429f82d7748bb1ad183ad4a0831c4c1701a7be867db2a5a0ff07bf601

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5346a210e81d3e37b97b49221d1afa73580e61e66a503ae35bd78478daa013a8
MD5 cafb542fa8a6274d08e35d251bcee9ed
BLAKE2b-256 479fa22ccaa2369e78743781725291ed63d8b1d509d02a16f27d9fe0e7e12088

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp310-cp310-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp310-cp310-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 23dd6560a0b5ae5e184519391b409e0079a0008495bdd349e2a59ea3dafcd43d
MD5 be66c46947671d0aa9be0a1c0198b0fc
BLAKE2b-256 2c4def29f6a046ef5b1cf4410b8c78eacfeb6dbfdd506607f7bbc109faf7d6e7

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e96f06f1b64011409256c2562f85e22e8d3f1434e839c3a0e1204bfb26854203
MD5 39cd19dd01260994754499771946f66e
BLAKE2b-256 1d9c1fc767583496295289dc7463dba57a3e159fa97483bd575aff78c3e33d57

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 8abbaa23709a9cdbe46c0620dcf06aab9b9323ca8335107cecdb4cd7ef4e3cdf
MD5 86d2b56582b168e3828368f3d93f93c2
BLAKE2b-256 bf7ce6cae53e49fda5e378568c6be0c21a26d67c38969202cc7570fc3db75167

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ccdbd3f561151323ff8eab11a2748760ff2d0b417b74c10f7478af8a9e4727e2
MD5 cfd2065aaaa96813b10c8766025351c4
BLAKE2b-256 48f9ffa6487da96e64bc28e954e03baa2a933ce6bca0fb1f1ff068f9283e5b63

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp39-cp39-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp39-cp39-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 0d8899f113a7a723bae8cc8b79d0e3da60d9ce14e5246079c0846cab4fdf44bf
MD5 5ebe32d20de1d98e400d546fa9dfeff8
BLAKE2b-256 80fb9caffd12a7fbe4e56cb84fa4ecfcf1b2541a052599985cd7e0c32a6f2d00

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 06fef0fbd105872a803cb34053fcc4431328b0e66946dd49ae0dfc6bc1ec2a83
MD5 6b012d94243e1a4331fec61b9373b416
BLAKE2b-256 268353caaa40884f7eef359d7e39c46d830364dfb11928701651af8b71bf1cd2

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 370b202a1f88a7f3ad539dbe159dae25e8c81a0bd954fde10b9c71993f9450ba
MD5 b2c335110815b433c65226a077331de3
BLAKE2b-256 24eb824b9bb59c20396202891b00954c5fc0fd3df10a0ad8de932b11e5e00cbf

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp38-cp38-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 182910c8ec7d3e14059a9d2affa072e75f8aeb715e0673de53587737930fa94c
MD5 0021cfc6b7f593acb077d3e94dc88d38
BLAKE2b-256 3540fe6322ba741b807eff256a1786b9d8839f664b3c227061d89962a48a9f52

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp38-cp38-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp38-cp38-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 0c5d0ed55e737f49324b2a9626c3e7672fb3eb31ee7669b3478c38ff65d1f6c4
MD5 6e06d158c7ee4a8ddccc47a45edb272c
BLAKE2b-256 d08626b9714567360a1728f41153b17be89e1f5004ffedd99cf78e49b83dd150

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7d4534d286ad899847ef24ce55260a927174f771c37db49cc354a010d1dcc5cf
MD5 a2cb8066820e6bdd037fef08209764da
BLAKE2b-256 256e3f590af8996e17ccf8461e8327b9e62c3cfeb1ba9eb5bcc4a25edba053a4

See more details on using hashes here.

File details

Details for the file cython_chess-0.3-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for cython_chess-0.3-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 25122ec49a9da6e6076592287d78c7a45dce026db10308a6a68cd16afee4bcf6
MD5 74507c55a9ad8a17304747f983a75ef4
BLAKE2b-256 2c6a4b2c01d6208a1a0d55509be62609519e35985cd14e69cf690f4516dab3f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page