[Python] Why is there not much speedup when using parallel processing during bubble sort?

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I want to compare the effect of multiprocessing for bubble sort.

    Let us consider first the original one without multiprocessing:

    import multiprocessing
    import random
    import time
    import numpy as np

    def bubble_sort(array):
    check = True
    while check == True:
    check = False
    for i in range(len(array) - 1):
    if array > array[i + 1]:
    check = True
    temp = array
    array = array[i + 1]
    array[i + 1] = temp
    print("Array sorted: ", array)

    if __name__ == "__main__":
    array = np.random.randint(0, 1000, 10000)
    start = time.time()
    bubble_sort(array)
    print("Time taken: ", time.time() - start)


    The result is:

    Array sorted: [ 0 0 0 ... 999 999 999]
    Time taken: 25.157966375350952


    Now with multiprocessing:

    if __name__ == "__main__":
    array = np.random.randint(0, 1000, 10000)
    p = multiprocessing.Process(target=bubble_sort, args=(array,))
    start = time.time()
    p.start()
    p.join()
    print("Time taken: ",time.time()-start)


    The result is:

    Array sorted: [ 0 0 0 ... 999 999 999]
    Time taken: 24.962100744247437


    There is only one second difference, am I missing something?

    Continue reading...

Compartilhe esta Página