[Python] How to improve my concurrent processing time in this code?

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have programmed the following code in Python:

    from concurrent.futures import ProcessPoolExecutor, \
    ThreadPoolExecutor, \
    as_completed
    import random
    import time

    def addS(v):
    s=0
    start=time.time()
    for i in range(0,len(v)):
    s=s+v
    start1=time.time()
    print ("sec time ",start1-start," sum is ",s)

    def dummyFun(l):
    s=0
    for i in range(0,len(l)):
    s=s+l
    return s

    def main():
    workers=4
    v = [random.randint(1,101) for _ in range(1000000)]
    addS(v)
    dim=(int)(len(v)/(workers*10))
    s=0

    chunks=(v[k:k+dim] for k in range(0,len(v),(int)(len(v)/(workers*10))))
    start=time.time()

    with ProcessPoolExecutor(max_workers=4) as executor:
    futures=[executor.submit(dummyFun,chunk) for chunk in chunks]

    start1=time.time()

    for future in as_completed(futures):
    s=s+future.result()
    print ("concurrent time ",start1-start," sum is ",s)


    when I use the ProcessPoolExecutor() my results are the following:

    sec time 0.06529831886291504 sum is 51004089
    concurrent time 0.5757372379302979 sum is 51004089


    I have also changed to ThreadPoolExecutor() and the results are:

    sec time 0.06471061706542969 sum is 50981197
    concurrent time 0.09298276901245117 sum is 50981197


    Also, when I set up max_workers parameter as None, I got the following results:

    sec time 0.06425285339355469 sum is 50983899
    concurrent time 0.09010934829711914 sum is 50983899


    How can I improve my concurrent time? because the sequential time keeps being faster all the time or is there a reason for this?

    Continue reading...

Compartilhe esta Página