Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is zlib.crc32 faster than binascii.crc32?

Tags:

python

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
like image 943
jl6 Avatar asked Jun 12 '17 15:06

jl6


1 Answers

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.

like image 72
Sean Summers Avatar answered Oct 18 '22 22:10

Sean Summers