[issue42942] Feature request: Add decdigest() to hashlib

Christian Heimes report at bugs.python.org
Sun Jan 17 06:31:15 EST 2021


Christian Heimes <lists at cheimes.de> added the comment:

Do you have any benchmarks that back up your claim that integers are faster than using digest or hexdigests? Python's str and bytes types are highly optimized.

Hash digests don't fit into native integers, because they are larger than uint64_t and therefore have to be converted into arbitrary size integers (aka bigints). Arbitrary size integers have an overhead. For example it's slower to convert bytes to an integer than to hex string. Comparison of long its takes about as much time as comparing bytes.

By the way int(h.hexdigest(), 16) is a slow and inefficient way to convert a hash digest into an integer. int.from_bytes(h.digest(), endian) is much faster.

$ ./python -m timeit -s "from hashlib import sha256; s = sha256()" "s.digest()"
500000 loops, best of 5: 450 nsec per loop
$ ./python -m timeit -s "from hashlib import sha256; s = sha256()" "s.hexdigest()"
500000 loops, best of 5: 615 nsec per loop
$ ./python -m timeit -s "from hashlib import sha256; s = sha256()" "int.from_bytes(s.digest(), 'big')"
500000 loops, best of 5: 809 nsec per loop
$ ./python -m timeit -s "from hashlib import sha256; s = sha256()" "int(s.hexdigest(), 16)"
200000 loops, best of 5: 1.03 usec per loop

----------
nosy: +christian.heimes, gregory.p.smith

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42942>
_______________________________________


More information about the Python-bugs-list mailing list