I am looking to get any type of improvement, I want to be able to hash a list of 'blocks' aka strings and check if hashes match per block, if you can help me with that, currently reading and writing is optimized as I am writing direct bytes, any type of improvement in performance is appreciated! I am also looking to use as little ram, when I read the file for confirmation I want to use as little ram as possible.
import multiprocessing
import time as t
from hashlib import sha256
def crypto(args: list):
block, requests = args
h = sha256(block).digest
n = b'\n'
with open("blocks.txt", "ab") as blocks:
write = blocks.write
for i in range(requests):
write(h() + n)
blocks.close()
if __name__ == "__main__":
print("")
print("Program starting... \n")
last_block, block, counter, requests, threads, confirmed = [], "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()".encode(
'UTF-8'), 0, 10000000, 3, False
args = [block, requests]
print("Clearing blocks.txt...")
with open("blocks.txt", "wb") as blocks:
blocks.write(b"")
blocks.close()
print("Cleared blocks.txt... \n")
print("Starting the pool...")
p = multiprocessing.Pool(processes=threads)
print("Pool has started... \n")
start_time = t.perf_counter()
print("Starting the hashing...")
p.map(crypto, [args] * threads)
p.close()
print("Hashed all requests... \n")
time = t.perf_counter() - start_time
print("Checking confirmation cycle...")
with open("blocks.txt", "rb") as blocks:
data = blocks.readlines()
for sha in data:
if data[0] == sha:
confirmed = True
else:
print(f"**Invalid Confirmation:{sha}**")
confirmed = False
break
blocks.close()
confirm = t.perf_counter() - time
print("Checked the confirmation cycle... \n")
if confirmed:
print("Valid confirmation cycle! \n")
else:
print("Invalid confirmation cycle! \n")
print(
f"Total time to process the {requests} requests with {threads} confirmations: {time}.")
print(
f"Average hashes per second per confirmation thread: {requests / time}."
)
print(f"Time to check if confirmations are legal: {confirm}.")
crypto()
actually do in the real world? Currently there's a trivial speedup by not computing the same block hash a very large number of times, but I'm presuming it will be hashing different blocks---i.e. you're not trying to stress test python's sha256 \$\endgroup\$