简体   繁体   中英

multiprocessing and python code

the execution of my python code is too slow, I have 7 processors and python uses only one, I just discovered the multiprocessing option but I have no idea how to use it, so could you modify my following code using this option?

from itertools import combinations

def New5(A,C5):
    d=True
    for a in A:
        if(d==True):
            d=(a not in C5)
    return d

def refcomb10(h):
    T=[]
    C5={0}
    b=0
    C10=combinations(range(h),10)
    for S in C10:
        A=combinations(S,5)
        if(New5(A,C5)):
            A=combinations(S,5)
            for a in A:
                C5.update({a})
            T.append(S)
            b+=1
            print(b,S)
    return([T,C5])

U=refcomb10(60)

Excuse my english

You probably don't need to multithread to fix your performance problem. These improvements may be more helpful:

  • remove print(b,S)
  • remove the second A=combinations(S,5) , if possible
  • replace

      for a in A: C5.append(a) 

    with

      C5.extend(A) 
  • profile your code, perhaps like this: https://docs.python.org/2/library/profile.html . Find what function is slow and try to make it faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM