0

I'm trying to implement multithreading and multiprocessing on a sorting algorithm. The way implemented it is by:

  • Initialize a list of 10k items
  • Assign a random variable between 0-100 for each element

    arr = [1] * 10000
    for x, i in enumerate(arr):
        arr[x] = random.randint(0, 100)
    t_arr = arr
    m_arr = arr
    s_arr = arr
    
  • Create 2 sub-lists -- one for values lower than or equal to 50 and one for the rest

I then used bubble sort for both sub-lists in parallel. One using threads and one using processes.

Theoretically both should be faster, but only multiprocessing is. Multithreading produces the same results.

I have already tried different sorting algos, problem is persistent.

# Threading Version
start_time = time.time()
subarr1 = []
subarr2 = []
# Split Array into 2
for i in t_arr:
    if i <= 50:
        subarr1.append(i)
    else:
        subarr2.append(i)
# Sort first array
t1 = threading.Thread(target=bubbleSort, args=(subarr1,))
# Sort second array
t2 = threading.Thread(target=bubbleSort, args=(subarr2,))
t1.start()
t2.start()
t1.join()
t2.join()
end_time = time.time() - start_time
print("--- %s seconds ---" % (end_time))


# Serial Version
start_time = time.time()
subarr1 = []
subarr2 = []
# Split Array into 2
for i in s_arr:
    if i <= 50:
        subarr1.append(i)
    else:
        subarr2.append(i)
# Sort first array
bubbleSort(subarr1)
# Sort second array
bubbleSort(subarr2)
end_time = time.time() - start_time
print("--- %s seconds ---" % (end_time))


# Multiprocessing Version
start_time = time.time()
subarr1 = []
subarr2 = []
# Split Array into 2
for i in s_arr:
    if i <= 50:
        subarr1.append(i)
    else:
        subarr2.append(i)
# Sort first array
p1 = multiprocessing.Process(target=bubbleSort, args=(subarr1,))
# Sort second array
p2 = multiprocessing.Process(target=bubbleSort, args=(subarr2,))
p1.start()
p2.start()
p1.join()
p2.join()
end_time = time.time() - start_time
print("--- %s seconds ---" % (end_time))
  • Multithreading: around 6 seconds
  • Serial: around 6 seconds (similar to Threading)
  • Multprocessing: around 3 seconds

These results are consistent. Any advice?

colidyre
  • 4,170
  • 12
  • 37
  • 53
  • Possible duplicate of [What are the differences between the threading and multiprocessing modules?](https://stackoverflow.com/questions/18114285/what-are-the-differences-between-the-threading-and-multiprocessing-modules) – ForceBru Aug 21 '19 at 16:42
  • Google Python GIL (global interpreter lock) and despair :/ – Jeremy Friesner Apr 12 '20 at 02:49

0 Answers0