[Python] Why does my Python multiprocessing code run slower than the single-threaded version?

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a CPU-bound task in Python and attempted to speed it up using the multiprocessing module. I expected performance improvements due to parallel execution, but instead, the multiprocessing version runs noticeably slower than the single-threaded version. I'm using multiprocessing.Pool with map() to distribute tasks across processes.

    Here's a simplified version of the code:

    from multiprocessing import Pool
    import time

    def compute(x):
    return sum(i * i for i in range(10_000))

    if __name__ == '__main__':
    data = range(100)

    start = time.time()

    with Pool() as pool:
    results = pool.map(compute, data)

    print("Time with multiprocessing:", time.time() - start)


    The same logic in a simple for loop without multiprocessing is significantly faster. I'm running this on a machine with 8 CPU cores. What could be causing this slowdown? Is it related to overhead in process creation or data serialization?

    Continue reading...

Compartilhe esta Página