Last active
December 13, 2024 09:31
-
-
Save Tishka17/0c56d6939df3c3610d6421e8abcf4297 to your computer and use it in GitHub Desktop.
Comparing hash calculation
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 hashlib | |
from timeit import timeit | |
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor | |
a = b'1 2 3 4' * 1024 * 1024 * 100 | |
funcs = [ | |
hashlib.sha1, | |
hashlib.sha256, | |
hashlib.sha384, | |
hashlib.sha512, | |
] | |
def seq(): | |
return [ | |
func(a).hexdigest() | |
for func in funcs | |
] | |
tpool = ThreadPoolExecutor(max_workers=4) | |
ppool = ProcessPoolExecutor(max_workers=4) | |
def apply(i): | |
return funcs[i](a).hexdigest() | |
def threaded(): | |
return list( | |
tpool.map(apply, range(len(funcs)))) | |
def processed(): | |
return list( | |
ppool.map(apply, range(len(funcs)))) | |
assert seq() == threaded() | |
assert seq() == processed() | |
number = 10 | |
print(timeit("seq()", globals=globals(), number=number)) | |
print(timeit("threaded()", globals=globals(), number=number)) | |
print(timeit("processed()", globals=globals(), number=number)) |
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 hashlib | |
from timeit import timeit | |
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor | |
data = [ | |
b'1 2 3 4' * 1024 * 1024 * 100, | |
b'2 2 3 4' * 1024 * 1024 * 100, | |
b'3 3 3 4' * 1024 * 1024 * 100, | |
b'4 4 4 4' * 1024 * 1024 * 100, | |
] | |
def seq(): | |
return [ | |
hashlib.sha512(a).hexdigest() | |
for a in data | |
] | |
tpool = ThreadPoolExecutor(max_workers=4) | |
ppool = ProcessPoolExecutor(max_workers=4) | |
def apply(a): | |
return hashlib.sha512(a).hexdigest() | |
def threaded(): | |
return list( | |
tpool.map(apply, data)) | |
def processed(): | |
return list( | |
ppool.map(apply, data)) | |
assert seq() == threaded() | |
assert seq() == processed() | |
number = 10 | |
print(timeit("seq()", globals=globals(), number=number)) | |
print(timeit("threaded()", globals=globals(), number=number)) | |
print(timeit("processed()", globals=globals(), number=number)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment