In my code the parfor is slower than for loop when using backslah with matrices.
1 view (last 30 days)
Show older comments
I have following code. I am using backslash for matrices and parfor loop is slower than for loop. What kind of problems are there in following code? Thank you so much.
A = radn(3000);
[n,~] = size(A);
I = eye(n);
c = [0,-1/5, 1/5,-1/10, 1/10];
b = [128/3, 20/9, 85/3, -15, -(515/9)];
E = zeros(n);
tic
for k = 1:5
E = E + b(k)*((I-c(k)*A)\(c(k)*A));
end
toc
tic
parfor k = 1:5
E = E + b(k)*((I-c(k)*A)\(c(k)*A));
end
toc
E=E+I;
my results with 8 workers are as follows:
Elapsed time is 2.656596 seconds
Elapsed time is 3.542917 seconds
0 Comments
Accepted Answer
Bruno Luong
on 15 Nov 2022
There is no problem.
There might be no benefit to run parfor on function that is designed with multithread and already exploit most resource of your CPU
Put parfor on top is just doing more harm than good.
7 Comments
Raymond Norris
on 15 Nov 2022
Just to be clear, the parfor loop is 2x faster than running for for-loop in single threaded mode, yes (not compared to multi-thread mode)?
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!