Jun-20-2020, 04:46 AM
(This post was last modified: Jun-20-2020, 04:47 AM by medatib531.)
Hello,
I'm trying to understand how multiprocessing-multithreading works. Suppose I have the following code (which is basically the idea behind mining). In this case I am using 2 cores and everything looks good.
However if I choose to use more cores, the CPU utilization drops. i.e. if I use 4 cores, it utilizes them at 50% each. If I use 8, it utilizes them at 25% and so on.
My CPU is i9-9980HK 8-core.
Thank you
I'm trying to understand how multiprocessing-multithreading works. Suppose I have the following code (which is basically the idea behind mining). In this case I am using 2 cores and everything looks good.
However if I choose to use more cores, the CPU utilization drops. i.e. if I use 4 cores, it utilizes them at 50% each. If I use 8, it utilizes them at 25% and so on.
My CPU is i9-9980HK 8-core.
Thank you
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 |
import hashlib import time import multiprocessing as mp def hash_string(string): """ Return a SHA-256 hash of the given string """ return hashlib.sha256(string.encode( 'utf-8' )).hexdigest() def mine(seed,quit,foundit): #start = time.time() while not quit.is_set(): test = hash_string( str (seed) + "Teststring" ) seed + = 1 if test[ 0 : 6 ] = = '000000' : print (test) print (seed) #print("Total time:" + str(time.time() - start)) foundit. set () break import multiprocessing as mp quit = mp.Event() foundit = mp.Event() start = time.time() #for i in range(int(mp.cpu_count()/2)): for i in range ( 2 ): p = mp.Process(target = mine, args = ((i + 2 ) * 100000000 , quit, foundit)) p.start() foundit.wait() quit. set () print ( "Total time:" + str (time.time() - start)) |