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
    

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 
    

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.1.tar.gz (109.2 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.1-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.1-cp311-cp311-musllinux_1_2_i686.whl (1.8 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ i686

cython_chess-0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (631.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

cython_chess-0.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (621.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

cython_chess-0.1-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.1-cp310-cp310-musllinux_1_2_i686.whl (1.8 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ i686

cython_chess-0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (614.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

cython_chess-0.1-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (609.8 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ i686

cython_chess-0.1-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.1-cp39-cp39-musllinux_1_2_i686.whl (1.8 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ i686

cython_chess-0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (612.8 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

cython_chess-0.1-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (606.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ i686

cython_chess-0.1-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.1-cp38-cp38-musllinux_1_2_i686.whl (1.8 MB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ i686

cython_chess-0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (626.4 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

cython_chess-0.1-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (617.7 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ i686

File details

Details for the file cython-chess-0.1.tar.gz.

File metadata

  • Download URL: cython-chess-0.1.tar.gz
  • Upload date:
  • Size: 109.2 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.1.tar.gz
Algorithm Hash digest
SHA256 bfaeef779ed6c5991946fe0cf35a21f40366ebc94d233efe675285b40f37d6f9
MD5 4ddbe788b596948c1e8dd3520aad0b14
BLAKE2b-256 c529160af8e4754bd0936294ef0dda8072ae3f364cce073d8673cb8f33ed9dfb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9d7763c5078cf01888cccee14e069d48278a5c5f39852d72450ea0e7c2148a44
MD5 41d5a285b761eb7a1b5def4f211139f4
BLAKE2b-256 815999dd4a91646fa787993126ea6a49dc7d9b55e796e60c1f8946de83db745e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp311-cp311-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 c7459b86fe5d05d81335737815c384ddaed6215874fd1d78920a242addb8c3d5
MD5 9d576f257f5319fad43a7d83ef1bda3c
BLAKE2b-256 d5410de2520a3c3edfcd388ed2d1c7017676e95057a3ca11a7b1cd500f33b3ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dd93e6bc8c534e47599475491b249841b8d415720fb846a3a0de0f04f54b7573
MD5 fed545cd9d464cf747a41574909a3dbf
BLAKE2b-256 b5f7eed59269468a93d1b65a158192d1a74b715ed8fb7e6fcaef7f894df676c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 b178357f8741af61390adaaf4f95199638c62cd1fc6355b9247a66ad274855c8
MD5 31ec4662b56d181044e28bdc65e4f183
BLAKE2b-256 e51a3c5bd5dddad3a4fd906ad5c028ee69f0acfdfb8ec0f0260a9d13b3d659d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c60ce3b8d5dac70795f9a34bd97e9867784a6970f5006dbed54ec16cb6e3848d
MD5 997c2aac60a13715811c62405c568652
BLAKE2b-256 362163f262be5a8537043968911e236054d57e297f47bf69f2ad970a37e3fb0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp310-cp310-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 82aa0627a4e76c20f181800cd0d683da7189514f1581af4f9bfbf7b6399738e8
MD5 7be059ba0b92b09d1a78fa7da820af21
BLAKE2b-256 1c3ea421e28c065b483732306f4a222ca0fc86c88f193558530c9d8b6902e907

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7bdd568d146e3c6764de6c1cc0388e8b906162a6bc936f4f91a1cd1a91e60fe4
MD5 0bb4ffb6b81ad5d94d76f40c83f0e63c
BLAKE2b-256 929cf4950e114aa7d7fc26ccb92db19b7715f14085565d55a83626414af071d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 370338b8836b184392bfd27b5ba6e112b71f70cb42229f382d26aa38d62d4189
MD5 2bc53dafec9f662663b7b3edc1567be6
BLAKE2b-256 e5c3b92e23f4f0b1a45eddd52237390aa28727df47f70a711bfb975b2f29a8c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 86b877abef6680bbd6ec4ce4b58311fb99513fc349a9ebbe29b8c478e040a44f
MD5 4e648a7ec3488782fca47f2e4b00972b
BLAKE2b-256 1c185306762c08ad9adf5d5ddcaf173411a9811ed7ca4b799676da2cea2a26f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp39-cp39-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 8f43fadb8aa46d0aa378956ac73200960518ae685e33bab45b9fdf51b76118b5
MD5 b0863093760b60f35a151e0b749d0570
BLAKE2b-256 119562a1550f4507ff67e8022fcfe1c1bd54d573c5b9892bccdacb0b99f84cd4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 11a261701664dedc50fe20e3911e708167468fb95357cd06ecbaa51bd8f36915
MD5 b88be88e3663181f590b33311bbebbb1
BLAKE2b-256 23e75deef9c596690aee04c4ee04fe8ec5e7ebe1f65d4e0908c3aabacd0df26b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 13f348a36716bec2e22fc9cd904549be34372d7e79c44521411254cf19c4862f
MD5 eb6d5ebb5ccb449200d8b066a572b79e
BLAKE2b-256 92da8fc777c299a52bab8070abfaf1b3c11805b3722270f04f293aee91ad37ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 85cc5aa1df76332b7a5ff817b27e6b2508b6f4fe883a138241d79ab3943f685b
MD5 b58d7767c21c36c5a83ad0d29e1601a3
BLAKE2b-256 343c406fb40ee5d595fb57c8bef0a241fd8e5f7feb464114b67b3ed51431e9e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp38-cp38-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 02cdf9f98b88415b6f19b97d94530068179f812b4fa00b3022ff7ac29f223d28
MD5 b9f47eb3c3856ce3e09bc7750397ceee
BLAKE2b-256 f891850b5d1f66019af7485f6a244a3ba28fb820c32658dc74975ab715d1d65e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 81cebd783756af3321ead5d6450e21557e9433e380328f03231b090d98dc6fea
MD5 c43cd595dd4961fd5439b59b76d40705
BLAKE2b-256 0b5a862009d41890388d9f868a465cd504d43452051e056acefd409f5453e1c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cython_chess-0.1-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 d3c713e2a5f6300cd729b86ea976ade753a68d3e319cf984c2125da7fdb1accf
MD5 df08229370af583775cdc2f312353053
BLAKE2b-256 2a58d422873bd6b4793b102e7f939bea2742784d9b094219f11baeeda95b3297

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