Skip to content

Instantly share code, notes, and snippets.

@rafalstapinski
Last active October 17, 2021 03:52
Show Gist options
  • Save rafalstapinski/c09db376225fb23a225f4ce98995df66 to your computer and use it in GitHub Desktop.
Save rafalstapinski/c09db376225fb23a225f4ce98995df66 to your computer and use it in GitHub Desktop.
quick python hashing func speed test
import random
import string
import time
from hashlib import blake2b, blake2s, md5, sha1, sha3_224, sha3_256, sha3_384, sha3_512, sha224, sha256, sha384, sha512
algs = [
sha512,
sha3_224,
sha1,
sha256,
md5,
sha224,
sha3_256,
blake2s,
sha384,
sha3_384,
blake2b,
sha3_512,
]
def generate_random_str(size: int) -> bytes:
return b"".join(random.choice(string.printable).encode("utf-8") for i in range(size))
def generate_random_set(set_size: int, item_size: int) -> list[bytes]:
return [generate_random_str(item_size) for i in range(set_size)]
def main():
randoms = generate_random_set(10000, 100)
random.shuffle(algs)
results = []
for alg in algs:
start = time.time()
[alg(r).hexdigest() for r in randoms]
results.append((alg, time.time() - start))
for res in sorted(results, key=lambda r: r[1]):
print(res[0], res[1], sep="\t")
if __name__ == "__main__":
main()
"""
max item size = 100
0.00422 <class '_blake2.blake2s'>
0.00463 <class '_blake2.blake2b'>
0.00686 <built-in function openssl_sha1>
0.00705 <built-in function openssl_md5>
0.00821 <built-in function openssl_sha384>
0.00852 <built-in function openssl_sha224>
0.00907 <built-in function openssl_sha512>
0.00975 <built-in function openssl_sha256>
0.01122 <built-in function openssl_sha3_384>
0.01127 <built-in function openssl_sha3_224>
0.01172 <built-in function openssl_sha3_256>
0.01532 <built-in function openssl_sha3_512>
max item size = 10000
0.10667 <built-in function openssl_sha1>
0.12501 <class '_blake2.blake2b'>
0.14361 <built-in function openssl_md5>
0.15192 <built-in function openssl_sha384>
0.15246 <built-in function openssl_sha512>
0.16491 <class '_blake2.blake2s'>
0.22134 <built-in function openssl_sha256>
0.22243 <built-in function openssl_sha224>
0.25736 <built-in function openssl_sha3_224>
0.27098 <built-in function openssl_sha3_256>
0.35185 <built-in function openssl_sha3_384>
0.48403 <built-in function openssl_sha3_512>
random item sizes between 0 and 10000
0.05733 <built-in function openssl_sha1>
0.06793 <class '_blake2.blake2b'>
0.07849 <built-in function openssl_md5>
0.08165 <built-in function openssl_sha384>
0.08221 <built-in function openssl_sha512>
0.08426 <class '_blake2.blake2s'>
0.11476 <built-in function openssl_sha224>
0.12041 <built-in function openssl_sha256>
0.13963 <built-in function openssl_sha3_224>
0.14127 <built-in function openssl_sha3_256>
0.18198 <built-in function openssl_sha3_384>
0.26708 <built-in function openssl_sha3_512>
tl;dr if picking naively on a 64bit machine, blake2b seems to be fastest choice
not really even considering md5 or sha1 but here for comparison
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment