Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Created January 17, 2024 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnor663/57ece045df23fe977228aae0cd3ae189 to your computer and use it in GitHub Desktop.
Save oconnor663/57ece045df23fe977228aae0cd3ae189 to your computer and use it in GitHub Desktop.
short-input hash benchmarks in Python
import time
from blake3 import blake3
from hashlib import sha256, sha512, blake2s
input_bytes = b"hello world"
print(f"input bytes: {input_bytes}")
warmup_iterations = 1_000
measure_iterations = 1_000_000
def bench(name, hasher):
for _ in range(warmup_iterations):
hasher(input_bytes).digest()
start = time.time()
for _ in range(measure_iterations):
hasher(input_bytes).digest()
end = time.time()
iteration_ns = round((end - start) / measure_iterations * 1e9)
print(f"average {name} iteration time: {iteration_ns} ns")
targets = [
("SHA-256", sha256),
("SHA-512", sha512),
("BLAKE2s", blake2s),
("BLAKE3 ", blake3),
]
for target_name, target_hasher in targets:
bench(target_name, target_hasher)
input bytes: b'hello world'
average SHA-256 iteration time: 284 ns
average SHA-512 iteration time: 470 ns
average BLAKE2s iteration time: 231 ns
average BLAKE3 iteration time: 355 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment