我在Matlab中运行了很多长时间的模拟,通常需要几分钟到几个小时,所以为了加快速度,我决定使用parfor循环同时运行模拟.
arglist = [arg1, arg2, arg3, arg4];
parfor ii = 1:size(arglist, 2)
myfun(arglist(ii));
end
Run Code Online (Sandbox Code Playgroud)
一切都运作得很好,除了一件事:进步印刷.由于每次模拟都需要花费很多时间,因此我通常使用类似的东西来打印进度
prevlength = 0;
for ii = 1:tot_iter
% Some calculations here
msg = sprintf('Working on %d of %d, %.2f percent done', ii, tot_iter, ii/tot_iter);
fprintf(repmat('\b', 1, prevlength))
fprintf(msg);
prevlength = numel(msg);
end
Run Code Online (Sandbox Code Playgroud)
但是,正如可以预料的那样,在parfor循环中执行此操作时,会出现混乱.
我用Google搜索了很多寻找一个解决方案,并已发现了一堆"PARFOR进步打印机"像这一个.但是,所有这些都打印整个parfor循环的进度,而不是显示每个迭代的进度.由于我在parfor循环中只有大约4-8次迭代,但每次迭代大约需要一个小时,这种方法对我来说并不是很有帮助.
对我来说理想的解决方案就是这样
Working on 127 of 10000, 1.27 percent done
Working on 259 of 10000, 2.59 percent done
Working on 3895 of …Run Code Online (Sandbox Code Playgroud)