From 1cc53ece7bdb808017221743eb11cc3300e76e7d Mon Sep 17 00:00:00 2001 From: Massimo Santini Date: Wed, 8 Jun 2022 17:51:12 +0200 Subject: [PATCH 1/2] Using bytes.hex instead of binascii.hexlify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To convert from bytes to an hex string the `bytes.hex` method is much faster than `str(hexlify(…).decode('uft-8'))`. $ python3 -m timeit "b'\x00\x00\x00\x00\x00\x19\xd6h\x9c\x08Z\xe1e\x83\x1e\x93O\xf7c\xaeF\xa2\xa6\xc1r\xb3\xf1\xb6\n\x8c\xe2o'.hex()" 5000000 loops, best of 5: 72.3 nsec per loop $ python3 -m timeit -s "from binascii import hexlify" "hexlify(b'\x00\x00\x00\x00\x00\x19\xd6h\x9c\x08Z\xe1e\x83\x1e\x93O\xf7c\xaeF\xa2\xa6\xc1r\xb3\xf1\xb6\n\x8c\xe2o').decode('utf-8')" 2000000 loops, best of 5: 137 nsec per loop $ python3 -m timeit -s "from binascii import hexlify" "str(hexlify(b'\x00\x00\x00\x00\x00\x19\xd6h\x9c\x08Z\xe1e\x83\x1e\x93O\xf7c\xaeF\xa2\xa6\xc1r\xb3\xf1\xb6\n\x8c\xe2o').decode('utf-8'))" 2000000 loops, best of 5: 194 nsec per loop --- blockchain_parser/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain_parser/utils.py b/blockchain_parser/utils.py index 18ce8d8..0967255 100644 --- a/blockchain_parser/utils.py +++ b/blockchain_parser/utils.py @@ -26,7 +26,7 @@ def double_sha256(data): def format_hash(hash_): - return str(hexlify(hash_[::-1]).decode("utf-8")) + return hash_[::-1].hex() def decode_uint32(data): From c334275239cfe7735f867f909a76d315d0aae963 Mon Sep 17 00:00:00 2001 From: Massimo Santini Date: Thu, 9 Jun 2022 15:26:27 +0200 Subject: [PATCH 2/2] Removed stale import Ditto. --- blockchain_parser/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/blockchain_parser/utils.py b/blockchain_parser/utils.py index 0967255..fd04a89 100644 --- a/blockchain_parser/utils.py +++ b/blockchain_parser/utils.py @@ -9,7 +9,6 @@ # modified, propagated, or distributed except according to the terms contained # in the LICENSE file. -from binascii import hexlify import hashlib import struct