- How many dedicated workers? Most often, more workers, the lower the amount of time.
- Amount of time to run the unit of work. Does it take more time to send the code then to run the code?
parfor slower than for
1 view (last 30 days)
Hi Community members,
I am trying to run a very simple parfor loop and a for loop to compare results. However, to my surprise parfor is almost 100 times slower than for loop. Can anyone please explain this? I intend to run a code with almost 10000000000 iterations and need to decide how to make it fastest. Your suggestions will be very helpful in this regard.
arr(x) = x;
arr(x) = x;
Raymond Norris on 20 Sep 2021
There are several considerations
Your example is trivial. If your code really takes 0.005s to run all your sims, then parfor is not needed. Conversely, here's a better trivial example
parfor idx = 1:50
Jan on 20 Sep 2021
The main work in you example is the iterative growing of the array. This is a waste of time in sequential and parallel code. Pre-allocate the output properly.
Starting parallel threads must take some time. For such a trivial code, the overhead is expected to be higher than the payload. Compare this with instructing 8 people to say the numbers 1 to 50. It is much faster to do this by your own.