Recommended steps to deal with a slow loop

1 回表示 (過去 30 日間)
Danielle Leblanc
Danielle Leblanc 2012 年 7 月 27 日
Hi,
If a "for loop" is slow and each iteration is taking 2 hours what are the recommended steps that I should follow to deal with this issue?

回答 (2 件)

Conrad
Conrad 2012 年 7 月 27 日
I would recommend that you first focus on what happens inside the for loop... why is it taking so long? Get a complete breakdown of how much time is spend at every step, then you can decide where you can possibly optimise.
Further details would be great.

Sean de Wolski
Sean de Wolski 2012 年 7 月 27 日
  • Listen to mlint. If it is warning you about a variable changing size within a loop, that will slow it down immensely
  • Remove every calculation from every loop that does not depend on the loop.
  3 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 7 月 27 日
Use the profiler to identify the bottlenecks. Just run two or three files with profiler and it will show you where to focus your efforts. Some obvious things:
  • try/catch is slow. I would try and write the code to avoid those.
  • Skip the call to find here:
x0=find(ismember(s2(:,1),ID(k,1)));
  • regexp can be slow, perhaps csvread or dlmread?
  • Conversions to and from cell is slow (s2=num2cell(nan(size(V,1)-1,size(scol,2)));) avoid this if possible.
Danielle Leblanc
Danielle Leblanc 2012 年 7 月 27 日
the profiler is showing that ismember and unique functions are th emost time consuming functions

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by