In the following benchmark, I find that the function zlib.crc32
is about 2.5 times faster binascii.crc32
. Why is that, and are there any downsides to using the zlib
module's implementation?
#!/usr/bin/python3
import timeit
print("b:", timeit.timeit("binascii.crc32(data)", setup="import binascii, zlib; data=b'X'*4096", number=100000))
print("z:", timeit.timeit("zlib.crc32(data)", setup="import binascii, zlib; data=b'X'*4096", number=100000))
Result:
b: 1.0176826480001182
z: 0.4006126120002591
I found this discussion: https://mail.python.org/pipermail/python-3000/2008-March/012728.html where Gregory P. Smith (in a discussion with Guido) wrote:
Removal from binascii would break things for platforms or embedded systems wanting crc32 that don't want to include zlib. Anyone care?
TL;DR: The binascii implementation is for systems that don't have zlib (or don't want to include it), so it's considered sub-optimal, but would break things if removed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With