Skip to content

Instantly share code, notes, and snippets.

@JohnAtl
Created August 9, 2024 20:19
Show Gist options
  • Save JohnAtl/e95a522a54492727c2dce2c28d2c8262 to your computer and use it in GitHub Desktop.
Save JohnAtl/e95a522a54492727c2dce2c28d2c8262 to your computer and use it in GitHub Desktop.
Command line program to compare MD5 and xxHash hashing speeds
#!/usr/bin/env python3
import hashlib
import xxhash
import timeit
import argparse
def hash_file_md5(filepath):
hash_md5 = hashlib.md5()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def hash_file_xxhash(filepath):
hash_xx = xxhash.xxh128()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_xx.update(chunk)
return hash_xx.hexdigest()
def measure_time(filepath):
md5_time = timeit.timeit(lambda: hash_file_md5(filepath), number=10)
xxhash_time = timeit.timeit(lambda: hash_file_xxhash(filepath), number=10)
print(f"MD5 hashing time: {md5_time:.6f} seconds")
print(f"xxHash128 hashing time: {xxhash_time:.6f} seconds")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Compare MD5 and xxHash128 hashing times.")
parser.add_argument("filename", type=str, help="The path to the file to be hashed.")
args = parser.parse_args()
measure_time(args.filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment