DPM
DPM

Reputation: 935

Is running multiple codes faster than running just one?

my question is rather simple: if I have a code that has a for cycle that iterates 150 times. Is it faster to split this into 3 files where each for cycle iterates 50 times? If so do I run the 3 codes at the same time or one at a time? Another option could be to use python multiprocessing in these 3 files, but the same question arises, do I run them all at once or one at a time?

Upvotes: 0

Views: 102

Answers (1)

tdelaney
tdelaney

Reputation: 77347

TL;DR - It depends.

In cpython, the Global Interpreter Lock (GIL) ensures that only 1 thread in a given process can run python code at a time. If your loop is CPU bound, adding threads in a single process doesn't speed it up. If your loop sleeps for some reason like reading data from disk, threads can speed it up. Using multiprocessing, since the GIL is per-process, you may speed up processing.

There is overhead to multithreading and multiprocessing, including the cost of starting the thread / process, getting data to the thread / process and returning the result. Depending on what you are doing, this may be more expensive than the loop itself.

So, there is no general answer to this question. It is highly task dependent.

Upvotes: 1

Related Questions