A fast bencode implementation in Cython that supports Python 3. It uses Cython's pure Python syntax.
Project description
Bencode.cy
A fast bencode implementation based on Cython, supporting Python 3.9+.
The bencode functions are 15% to 230% faster than other compiled libraries, and the decode methods are 40% to 320% faster than other compiled libraries.
On average, they outperform pure Python versions by 250%.
Based on my tests, it is currently among the faster bencode libraries available.
It is written using Cython's pure Python syntax, so it can run directly without Cython compilation. Of course, performance is significantly better after compilation.
Installation
pip install bencode.cy
or, if you use uv (it might be better):
uv add bencode.cy
Usage Examples
from bencode import bencode, bdecode, compiled
assert True == compiled # Check if the code has been compiled with Cython
assert bencode("WWWWWW") == b'6:WWWWWW'
assert bencode(233) == b'i233e'
with open("debian-8.3.0-amd64-netinst.iso.torrent", "rb") as f:
torrent = bdecode(f.read())
print(torrent[b'announce'])
Speed Benchmark
bencode.cy.encode and bencode.cy.decode are the Cython-compiled versions of this library. bencode.cy.encodepy and bencode.cy.decodepy are the Python versions. They are the same code—one compiled, one not—which is quite interesting.
bencoder.pyx.encode and bencoder.pyx.decode are the encoding/decoding functions from the bencoder.pyx library. However, it currently only supports up to Python 3.11 and is slightly slower, though I learned a lot from its code.
fastbencode.encode and fastbencode.decode are from another excellent library, fastbencode, which is written in Rust.
better_bencode.encode and better_bencode.decode are a C version of the bencoder library from better_bencode, but I don't know why it is so slow. Maybe Python 3.11 on macOS wasn't compiled to C.
bencodepy.encode and bdecodepy.decode are part of a pure Python version of the bencoder library from BencodePy.
From the comparison, this library is currently among the fastest options. You can see more detailed results below.
deocde benchmar result
| Name | OPS (Kops/s) | Mean | StdDev | Min | Max | Median | IQR | Outliers | Rounds | Iterations |
|---|---|---|---|---|---|---|---|---|---|---|
| bencode.cy.decode | 521.4145 (1.0) | 1.9179 (1.0) | 0.2650 (1.0) | 1.7500 (1.0) | 36.3330 (1.0) | 1.8750 (1.0) | 0.0410 (1.0) | 2174;3385 | 40001 | 1 |
| bencode2.decode | 490.4606 (0.94) | 2.0389 (1.06) | 1.0887 (4.11) | 1.8750 (1.07) | 201.3750 (5.54) | 2.0000 (1.07) | 0.0420 (1.02) | 1082;12577 | 134085 | 1 |
| bencode_rs.decode | 485.7196 (0.93) | 2.0588 (1.07) | 0.5006 (1.89) | 1.8750 (1.07) | 64.9580 (1.79) | 2.0410 (1.09) | 0.0420 (1.02) | 1664;16625 | 153847 | 1 |
| bencoder.pyx.decode | 191.7128 (0.37) | 5.2161 (2.72) | 0.6630 (2.50) | 4.8750 (2.79) | 95.9590 (2.64) | 5.1660 (2.76) | 0.0840 (2.05) | 1254;5630 | 68185 | 1 |
| bencode.cy.decodepy | 164.2124 (0.31) | 6.0897 (3.18) | 0.6805 (2.57) | 5.6670 (3.24) | 97.4160 (2.68) | 6.0420 (3.22) | 0.1250 (3.05) | 829;2364 | 66116 | 1 |
| better_bencode.decode | 134.0034 (0.26) | 7.4625 (3.89) | 0.8133 (3.07) | 7.0000 (4.00) | 84.9580 (2.34) | 7.4170 (3.96) | 0.0840 (2.05) | 499;3408 | 34238 | 1 |
| fastbencode.decode | 123.1785 (0.24) | 8.1183 (4.23) | 1.1346 (4.28) | 5.0000 (2.86) | 107.4170 (2.96) | 8.0000 (4.27) | 0.2920 (7.12) | 1585;3315 | 63493 | 1 |
| bencodepy.decode | 103.0055 (0.20) | 9.7082 (5.06) | 0.7732 (2.92) | 9.1660 (5.24) | 84.0830 (2.31) | 9.6670 (5.16) | 0.1670 (4.07) | 636;1559 | 39089 | 1 |
| bcoding.decode | 53.2570 (0.10) | 18.7769 (9.79) | 1.7134 (6.46) | 15.4580 (8.83) | 90.7500 (2.50) | 18.9580 (10.11) | 0.5010 (12.21) | 1638;1730 | 11205 | 1 |
encode benchmar result
| Name | OPS (Kops/s) | Mean | StdDev | Min | Max | Median | IQR | Outliers | Rounds | Iterations |
|---|---|---|---|---|---|---|---|---|---|---|
| bencode.cy.encode | 287.5983 (1.0) | 3.4771 (1.0) | 0.6224 (1.27) | 3.2500 (1.0) | 57.9590 (1.0) | 3.4170 (1.0) | 0.0430 (1.0) | 565;8949 | 57143 | 1 |
| bencoder.pyx.encode | 256.2142 (0.89) | 3.9030 (1.12) | 0.4911 (1.0) | 3.6660 (1.13) | 93.9170 (1.62) | 3.8750 (1.13) | 0.0820 (1.91) | 1054;3805 | 97163 | 1 |
| bencode.cy.encodepy | 188.4005 (0.66) | 5.3078 (1.53) | 0.5279 (1.08) | 5.0000 (1.54) | 72.0420 (1.24) | 5.2910 (1.55) | 0.0830 (1.93) | 1188;8115 | 83620 | 1 |
| bencode2.encode | 183.6796 (0.64) | 5.4443 (1.57) | 2.0376 (4.15) | 3.5840 (1.10) | 68.7500 (1.19) | 4.5000 (1.32) | 2.7090 (62.98) | 89;18 | 3999 | 1 |
| bencode_rs.encode | 145.0679 (0.50) | 6.8933 (1.98) | 1.5164 (3.09) | 4.3330 (1.33) | 60.3750 (1.04) | 7.4590 (2.18) | 2.5420 (59.09) | 3136;32 | 10601 | 1 |
| bcoding.encode | 140.0668 (0.49) | 7.1394 (2.05) | 1.3146 (2.68) | 6.6670 (2.05) | 100.0000 (1.73) | 7.0410 (2.06) | 0.1660 (3.86) | 410;1235 | 22494 | 1 |
| better_bencode.encode | 132.9986 (0.46) | 7.5189 (2.16) | 0.6240 (1.27) | 7.0830 (2.18) | 69.4170 (1.20) | 7.4590 (2.18) | 0.1260 (2.93) | 754;3475 | 53571 | 1 |
| fastbencode.encode | 93.0336 (0.32) | 10.7488 (3.09) | 2.6568 (5.41) | 8.2500 (2.54) | 94.7080 (1.63) | 9.2080 (2.69) | 5.0420 (117.21) | 7655;25 | 23977 | 1 |
| bencodepy.encode | 75.6976 (0.26) | 13.2105 (3.80) | 29.4086 (59.88) | 8.3330 (2.56) | 4,704.4170 (81.17) | 9.2500 (2.71) | 0.3340 (7.76) | 1831;3331 | 51065 | 1 |
Test Environment: Python 3.11.13, MacBook Air M2, macOS 26.01, October 2025
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
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bencode_cy-1.2.2.tar.gz.
File metadata
- Download URL: bencode_cy-1.2.2.tar.gz
- Upload date:
- Size: 81.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43eb4cc53ba5bee249524e8c29da948678a1aabd410eae6c8752a9ba8d0950d1
|
|
| MD5 |
5191d01e6ec0982851f61778e011f61e
|
|
| BLAKE2b-256 |
92c9a51d3e858fd8937670bee111ba3bd38aa97acd40070512e5c9f1424283d1
|
File details
Details for the file bencode_cy-1.2.2-cp314-cp314t-win_amd64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp314-cp314t-win_amd64.whl
- Upload date:
- Size: 109.1 kB
- Tags: CPython 3.14t, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64e9ba3f2262ab9bb90b25056502e3ff5e869e779968fa3c159da5a00f8830e6
|
|
| MD5 |
88b87d3fa6efe1ca8d8fbda6c64b230a
|
|
| BLAKE2b-256 |
0616a7a7e8f7618fe7684baeaae14713c7d14c7172b3c43bfd6283cb02b3d0c3
|
File details
Details for the file bencode_cy-1.2.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl
- Upload date:
- Size: 283.3 kB
- Tags: CPython 3.14t, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d26682ccd60e8ec97104833475ea6f02f834bb7f1ff575962b68bc972e30463d
|
|
| MD5 |
c9cd5e1dd667d4a57ba4bd853111fd18
|
|
| BLAKE2b-256 |
e3b9ba788c7244aa6dbaf56b75642947fafe68efbed511cfbdf476535602bfc0
|
File details
Details for the file bencode_cy-1.2.2-cp314-cp314t-macosx_11_0_arm64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp314-cp314t-macosx_11_0_arm64.whl
- Upload date:
- Size: 106.0 kB
- Tags: CPython 3.14t, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b32e68cd7123232701e773d72bd940610b320c2ba72fe60aab56332e3b1e35b
|
|
| MD5 |
b52ea691d4fe43fa183c8b9c290273c2
|
|
| BLAKE2b-256 |
10dbb33c10994aeeeb5cf2bd60d7593fc2039e03dc1f12b150f669e956434a4e
|
File details
Details for the file bencode_cy-1.2.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl
- Upload date:
- Size: 254.1 kB
- Tags: CPython 3.14, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3fd7157280faf22bfd13e0db5a54300f87a4d2f6817f9673a5d5e53fc6139b7
|
|
| MD5 |
74fecc615cab7f3bf4917339f593dc0b
|
|
| BLAKE2b-256 |
392bf21bdb6a56deb7ec7c54ea43d1833c0300e016c264c88490e127a7827e73
|
File details
Details for the file bencode_cy-1.2.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl
- Upload date:
- Size: 255.7 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aee1dcbe1e7cffadc8ee91e76f0c94deb73306421b59a28607ce5daac3d6ee43
|
|
| MD5 |
fa188b71329d9a395da427820838a68b
|
|
| BLAKE2b-256 |
bc2b6ab9503f5043a11cbfe3d100ae4a68fff88015204a2a140ccc179ca9b479
|
File details
Details for the file bencode_cy-1.2.2-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 105.3 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c282722ffbfda126556174d0f1cd5f1c4e9e96331ac34a8260955a8ee979d585
|
|
| MD5 |
61a6a87c8bb19b62be51787f4614812b
|
|
| BLAKE2b-256 |
9d00bf6ba4c6eab1408cae659537de1c19550e666ba042c5f7e991378129c6e9
|
File details
Details for the file bencode_cy-1.2.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl
- Upload date:
- Size: 257.8 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b04525bcac62937d035f057399bb59738e5073809d87e945afb72933a2efcb42
|
|
| MD5 |
6d6b2e3bd664f95b43224d9b9d486fc9
|
|
| BLAKE2b-256 |
968c2176fd5727d068601f53f71e6e94f4d069402f1df235ef5d1affe428b5b9
|
File details
Details for the file bencode_cy-1.2.2-cp312-cp312-macosx_15_0_arm64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp312-cp312-macosx_15_0_arm64.whl
- Upload date:
- Size: 104.5 kB
- Tags: CPython 3.12, macOS 15.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
970eb7cab92173e8b4f4679f33d1af72d1882b51f77cf42caa7970b1418c170c
|
|
| MD5 |
d662ff7e5348f456fce34b07f8aa6107
|
|
| BLAKE2b-256 |
6dd0fe0fc960e2bfcc0eefb109a2e735316486dfdbd8028212cd66ac4ce35eea
|
File details
Details for the file bencode_cy-1.2.2-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 104.7 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5146c596973b7f59f1bc91d7b57ac421bfbb1370426924fd6424cbdd8674a6f
|
|
| MD5 |
afb7a12452170829e98db6cb2a2f0ffd
|
|
| BLAKE2b-256 |
cf4839c9bd13d9140309f12389f1478f002080996cef2ebf94a6eff5b23315d9
|
File details
Details for the file bencode_cy-1.2.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl
- Upload date:
- Size: 251.7 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
810835bc43c2b8ed1321cc28c658ec881f2995341db9f0aceba79ce69b879893
|
|
| MD5 |
caf7e8d50a5264de88dd6285fed966d5
|
|
| BLAKE2b-256 |
be6842d60b15b6ae6ea5cbcf87ec761bf30b4b810033c5dff9bbcbbf4becb7ca
|
File details
Details for the file bencode_cy-1.2.2-cp311-cp311-macosx_10_9_universal2.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp311-cp311-macosx_10_9_universal2.whl
- Upload date:
- Size: 130.8 kB
- Tags: CPython 3.11, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3eacd0929b1357d49991f60e89684c86522966c76d387abe09df9228814e7fc
|
|
| MD5 |
f14d90b3e7fd8ce9b3c75737cbf89e15
|
|
| BLAKE2b-256 |
79166e245b48b7d0892ca90a76e93fefcc6a1e69ed41cb1bb7058d95602e68b3
|
File details
Details for the file bencode_cy-1.2.2-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 104.8 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ec150d5fabec99845d53d1aeb747365ac38a903d45dad8e9c6e663dd30ce706
|
|
| MD5 |
3f2438a7328141b741e643a545e2ccb4
|
|
| BLAKE2b-256 |
29921a3d2a43bccc6b6fb7a1a7162b543233dd50b9965cf36bf9e0aa48a7d2b8
|
File details
Details for the file bencode_cy-1.2.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl
- Upload date:
- Size: 236.7 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86f981b17876ba6c692624b0334ef5292faf18eb10281f2a8e74d3020bd6e664
|
|
| MD5 |
8deee0ed2543b536fdcc5bf891e4d1d4
|
|
| BLAKE2b-256 |
978fc9a361a998ca31d7dfb41e3a306badfd755d6df64acd42bac2ae67eb1fb5
|
File details
Details for the file bencode_cy-1.2.2-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 104.1 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd44d3dc4851e7d9fb9e2ec409294fd9e8473782ebbaeec078b02fc8777eba34
|
|
| MD5 |
9b57f0d2096d9cb7c682a46b594395d1
|
|
| BLAKE2b-256 |
555196d158177a34893ae2d603a65548be032556af67f60921ae888a7c34e01e
|
File details
Details for the file bencode_cy-1.2.2-cp39-cp39-win_amd64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 104.8 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59990540390968e2b1b5cec45e3b60e197e367ee895d81a13fd1f519a7017d29
|
|
| MD5 |
2b1c990f07e92e2ded634e2945cd04e7
|
|
| BLAKE2b-256 |
88c871d98a21a233b056b1e90fc724912265f2e9827fc8c75064cd09c70a4f35
|
File details
Details for the file bencode_cy-1.2.2-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_34_x86_64.whl
- Upload date:
- Size: 235.8 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1627e772305abe93f09eb59c5ddd8b39a867e7cbaf28827dae3471de9b446682
|
|
| MD5 |
ed15893f4166102a8b4749a1950ed8f2
|
|
| BLAKE2b-256 |
87755fa72009bdde224cd3610c7f949a3eb4e1f9766f66881fe5de1ac630c649
|
File details
Details for the file bencode_cy-1.2.2-cp39-cp39-macosx_10_9_universal2.whl.
File metadata
- Download URL: bencode_cy-1.2.2-cp39-cp39-macosx_10_9_universal2.whl
- Upload date:
- Size: 130.4 kB
- Tags: CPython 3.9, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6fa2338fa9a7303df2a0f6b8c064761ada021b7224a4e86b58052f9d5955c395
|
|
| MD5 |
b7fe2721e3b0655f2ba25326c2584fa4
|
|
| BLAKE2b-256 |
f8b2fea7ab167ff8618bd8823a1b1b150af9a84aa800b47a3ef8234589accb16
|