A parfor-loop can provide significantly better performance than a for-loop because several workers can compute simultaneously on the same loop. The parfor-loop executes statements in the loop body in parallel.
parfor-loops are more useful with a computationally demanding task or a task with many loop iterations such as a Monte Carlo simulation.
You cannot use a parfor-loop when an iteration in your loop depends on the results of other iterations. Each iteration must be independent. This is because parfor-loop iterations have no guaranteed order, unlike a for-loop where each iteration is sequential.
Note the value of d and i afterword.
The parfor-loop works because each element is determined by the indexed loop variable only and does not depend on other variables.
More examples of parfor-loops can be found here.
The Parallel Computing Toolbox allows you to scale up your workflow by running multiple workers in a parallel pool.
An example of when to use parpool is when running a parfor-loop. Lets say the loop has 20 iterations. If we assign the number of workers to be 20, then each worker will work on an iteration at the same time reducing the amount of time it takes for the loop to finish.
Note that the default number of workers is 12. The maximum number of workers allowed, is the number of cores in the machine. If this is exceeded, you may find your parfor loops slow down rather than speed up.
Examples on how to create a parpool
More information on parpool can be found here.
Parfeval allows you to evaluate a function in the background without waiting for it to complete.
This example shows how you can use parfeval to evaluate a function in the background and to collect results as they become available. In this example, you submit a vector of multiple future requests in a for-loop and retrieve the individual future outputs as they become available.
More examples and information about parfeval can be found here.
For more information on the MATLAB Parallel Computing Toolbox please visit here.