Christian Heimes <li...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue42942>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to