Why is parfor-loop is much slower than for-loop in this case?
4 次查看(过去 30 天)
显示 更早的评论
I just started to explore the pros and cons of parfor-loop. In the simpy testing codes below, parfor-loop is much slower than for-loop. I don't understand why. Any feedbacks are appreciated. (My project is to process a large number of tables and collect the parsing data into a huge long table.)
for loop
(In my 8-core Mac, the elapsed time is in the order of 0.004 seconds.)
C = cell(2,1);
tic
for i = 1:2
A = rand(1,10);
T = array2table(A);
C{i} = T;
end
toc
parfor-loop
(In my 8-core Mac, the elapsed time is in the order of 0.03 seconds.)
D = cell(2,1);
tic
parfor i = 1:2
A = rand(1,10);
T = array2table(A);
D{i} = T;
end
toc
0 个评论
采纳的回答
Image Analyst
2023-3-25
There is some setup time required to set up the different CPUs. If you have only 2 iterations and are using such tiny variables like you are, it's usually/mostly not worth it. Try with much larger variables and millions of loop iterations and see if it's better.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!