Created
January 17, 2024 19:39
-
-
Save oconnor663/57ece045df23fe977228aae0cd3ae189 to your computer and use it in GitHub Desktop.
short-input hash benchmarks in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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