-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
40 lines (34 loc) · 1.23 KB
/
benchmark.py
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
import hashlib
from datetime import datetime
import os
def benchmark_hash_function():
for i in range(1, 65536, 1000):
content = b"a" * i
begin = datetime.now()
hash_function = hashlib.md5()
hash_function.update(content)
hash_result = hash_function.hexdigest()
time_taken = datetime.now() - begin
print (f"{i},{time_taken.total_seconds() * 1000}")
def benchmark_file_hash():
hash_function = hashlib.md5()
for i in range(2, 50):
content = b"a" * 65536 * i
with open(f"benchmark-file-{i}.tmp", "w+b") as f:
f.write(content)
for j in range(2, 50):
with open(f"benchmark-file-{j}.tmp", "rb") as f:
begin = datetime.now()
f.seek(0, os.SEEK_END)
size = f.tell()
hash_function = hashlib.md5()
f.seek(0, os.SEEK_SET)
data = f.read(65536)
hash_function.update(data)
f.seek(-65536, os.SEEK_END)
data = f.read(65536)
hash_function.update(data)
hash_result = hash_function.hexdigest()
time_taken = datetime.now() - begin
print (f"{size},{time_taken.total_seconds() * 1000}")
benchmark_file_hash()