why parfor-loop spend more time for-loop?

1 回表示 (過去 30 日間)
Tao
Tao 2014 年 4 月 15 日
コメント済み: Tao 2014 年 4 月 16 日
function test_parfor
N = 1e8;
sum_all = 0; % sum all numbers
sum_odd = 0; % sum odd numbers
sum_even = 0; % sum even numbers
tic;
parfor i = 1 : N
sum_all = sum_all + i; % sum all numbers
if mod(i,2)
sum_odd = sum_odd + i; % sum odd numbers
else
sum_even = sum_even + i; % sum even numbers
end %endif
end %endfor
toc;
fprintf('sum_all=%d,\nsum_odd=%d,\nsum_even=%d.\n', ...
sum_all, sum_odd, sum_even);
I have initialized the parpool envionment and run the codes above. However, the parfor-loop took far more time than single for-loop. Why? What is wrong with my codes? Thank you very much! :-)

回答 (1 件)

Matt J
Matt J 2014 年 4 月 15 日
Did you execute it in a script or a function? Try the latter.
  3 件のコメント
Matt J
Matt J 2014 年 4 月 15 日
How many cores on your machine? And how many workers in your parpool?
Tao
Tao 2014 年 4 月 16 日
Thank you for your reply. The numCores of my PC is 12. I initialize 12 workers.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by