This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author christian.heimes
Recipients 2d4d, christian.heimes, gregory.p.smith
Date 2021-01-17.11:31:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1610883075.17.0.812556237167.issue42942@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2021-01-17 11:31:15christian.heimessetrecipients: + christian.heimes, gregory.p.smith, 2d4d
2021-01-17 11:31:15christian.heimessetmessageid: <1610883075.17.0.812556237167.issue42942@roundup.psfhosted.org>
2021-01-17 11:31:15christian.heimeslinkissue42942 messages
2021-01-17 11:31:14christian.heimescreate