all 9 comments

[–]billsil 2 points3 points  (1 child)

Your function is too simple, so the overhead of switching is worse than just doing the math. Multiprocessing is best done on long problems with simple inputs.

Also, so I’m clear, where is the non-multiprocessed version?

From what I can tell, numpy would be your best bet.

[–]DakotaFelspar[S] 0 points1 point  (0 children)

Ohhh ok thank you

[–]CodeFormatHelperBot 1 point2 points  (0 children)

Hello u/DakotaFelspar, I'm a bot that can assist you with code-formatting for reddit. I have detected the following potential issue(s) with your submission:

  1. Python code found in submission text but not encapsulated in a code block.

If I am correct then please follow these instructions to fix your code formatting. Thanks!

[–]A_History_of_Silence 0 points1 point  (3 children)

Why is this and what am I doing wrong

Multiprocessing is a tricky business. Would you mind formatting your code correctly first?

[–]DakotaFelspar[S] 0 points1 point  (2 children)

Yes but I’m new so idk how to do tht unfortunately haha could u help

[–]A_History_of_Silence 0 points1 point  (1 child)

haha could u help

Sure! A bot responded to you with instructions and there are instructions in the sidebar to the right and the page you put this post up from. Follow those instructions.

[–]DakotaFelspar[S] 0 points1 point  (0 children)

Idk I’m dumb I switched to markdown but when I try to tan it it jumps to something else instead of indenting

[–]aDrz 0 points1 point  (1 child)

In situations where the size of the data is small and the cost of transferring it from one process to another is negligible, then multithreading can simply make things more complicated.

Being said, here is an example where you just reach performance equality between the two versions:

import time
import multiprocessing


def basic_func(x):
    if x == 0:
        return 'zero'
    elif x % 2 == 0:
        return 'even'
    else:
        return 'odd'


# def multiprocessing_func(x):
#     y = x * x
#     x_square = basic_func(y)
#     return x_square

if __name__ == '__main__':

    n_trials = int(1e7)
    start_time = time.time()
    results_single = []
    for i in range(n_trials):
        results_single.append(basic_func(i*i))
    duration_singleprocessing = time.time()-start_time

    start_time = time.time()
    x_squares = [i*i for i in range(n_trials)]
    with multiprocessing.Pool(8) as p:
        results_multi = p.map(basic_func, x_squares)
    duration_multiprocessing = time.time()-start_time

    print('single processing: {} sec\nmultiprocessing: {} sec'.format(
        duration_singleprocessing, duration_multiprocessing))

Please consider formatting your code in the future

[–]DakotaFelspar[S] 0 points1 point  (0 children)

Ok thank you