-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_hashes.py
More file actions
executable file
·55 lines (46 loc) · 1.48 KB
/
benchmark_hashes.py
File metadata and controls
executable file
·55 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
"""Benchmark hash algorithms available in Python's hashlib."""
import hashlib
import time
import os
import platform
def benchmark_algorithm(algo: str, data: bytes) -> tuple[float, float]:
"""Benchmark a single algorithm, return (elapsed_seconds, mb_per_second)."""
h = hashlib.new(algo)
start = time.perf_counter()
h.update(data)
h.hexdigest()
elapsed = time.perf_counter() - start
size_mb = len(data) / (1024 * 1024)
return elapsed, size_mb / elapsed
def main():
size_mb = 100
data = os.urandom(size_mb * 1024 * 1024)
algorithms = [
'md5',
'sha1',
'sha256',
'sha512',
'sha3_256',
'blake2b',
'blake2s',
]
print(f"System: {platform.system()} {platform.machine()}")
print(f"Python: {platform.python_version()}")
print(f"Data size: {size_mb}MB\n")
print(f"{'Algorithm':<12} {'Time (s)':<10} {'Speed (MB/s)':<12}")
print("-" * 36)
results = []
for algo in algorithms:
try:
elapsed, speed = benchmark_algorithm(algo, data)
results.append((algo, elapsed, speed))
print(f"{algo:<12} {elapsed:<10.3f} {speed:<12.1f}")
except Exception as e:
print(f"{algo:<12} ERROR: {e}")
# Show ranking
print("\nRanked by speed:")
for i, (algo, _, speed) in enumerate(sorted(results, key=lambda x: -x[2]), 1):
print(f" {i}. {algo}: {speed:.1f} MB/s")
if __name__ == "__main__":
main()