Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

python - Why is multiprocessing slower in my Pool function than my non Pool function?

I wrote the following multitprocessing application and i have it's non Pool counterpart. The Pool version is awfully slow, but i can't figure out why. I tried some debugging techniques with time.time() which i left in the code. From what i can see, it shouldn't be slow, but is. Is anyone familiar with multiprocessing to know what i'm doing incorrectly here?

(You can replace the gmpy2 calls with math. if you have python 3.8 and not gmpy2)

import time
import gmpy2

def get_mod_congruence(root, N, withstats=False):
  r = root - N 
  if withstats==True:
    print(f"{root} ≡ {r} mod {N}")
  return r

def factorise(N):
  N = gmpy2.mpz(N)
  if N%2 == 0:
    return 2
  Nsqrt = gmpy2.isqrt(N)
  x=gmpy2.mpz(1)
  while True and x < Nsqrt:
    Nmodcongruence = get_mod_congruence((Nsqrt+x)**2, N)
    Ngcd = gmpy2.gcd(Nsqrt+x-gmpy2.isqrt(Nmodcongruence), N)
    if Ngcd != 1:
      break
    x+=1
  return Ngcd
 
def Ngcd(arglist):
   Nsqrt = arglist[0]
   x = arglist[1]
   Nmodcongruence = arglist[2]
   N = arglist[3]
   return gmpy2.gcd(Nsqrt+x-Nmodcongruence, N)

def factorise(N):
  N = gmpy2.mpz(N)
  if N%2 == 0:
    return 2
  Nsqrt = gmpy2.isqrt(N)
  x=gmpy2.mpz(1)
  while True and x < Nsqrt:
    Nmodcongruence = get_mod_congruence((Nsqrt+x)**2, N)
    Ngcd = gmpy2.gcd(Nsqrt+x-gmpy2.isqrt(Nmodcongruence), N)
    if Ngcd != 1:
      break
    x+=1
  return Ngcd
  
  
def factorisethreaded(N):

  from multiprocessing import Pool
  
  cpus = 4
  N = gmpy2.mpz(N)
  if N%2 == 0:
    return 2
  Nsqrt = gmpy2.isqrt(N)
  x=1 #gmpy2.mpz(1)
  while True and x < Nsqrt:
    start = time.time()
    arglist=[]
    for xx in range(x, cpus+x):
       Nmodcongruence = get_mod_congruence((Nsqrt+xx)**2, N)
       arglist.append([Nsqrt,xx,gmpy2.isqrt(Nmodcongruence), N])
       #print(xx, 1)
    end = time.time()
    #print(end-start)
    
    start = time.time()
    with Pool(cpus) as p:
       Ngcdresults=p.map(Ngcd, arglist)
    end = time.time()
    #print(end-start, len(arglist)) 
    
    start = time.time()
    Ngcdcheck = max(Ngcdresults)
    if Ngcdcheck > 1:
      return Ngcdresults[Ngcdresults.index(Ngcdcheck)]
    x+=cpus
    end = time.time()
    #print(end-start, x)

  return 1  
  
factorise(1009*7013)
# 1009
factorisethreaded(1009*7013)
# 1009
question from:https://stackoverflow.com/questions/65647459/why-is-multiprocessing-slower-in-my-pool-function-than-my-non-pool-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...