speed up for loop
15 ビュー (過去 30 日間)
古いコメントを表示
My code comprises of for loop which has up to 100 and sometimes 1000 increments and processing takes a long time. Is there a way to speed up this code? Please guide me
5 件のコメント
Image Analyst
2015 年 3 月 22 日
And did you use profiler like Stephen suggested or click the "Run and time" button? If not, why not?
回答 (1 件)
Stephen23
2015 年 3 月 21 日
編集済み: Stephen23
2015 年 3 月 21 日
Although you have not given any details at all of your code, there are several ways you can speed up your code:
- use vectorized code rather than loops.
- preallocate arrays before loops.
- use logical indexing rather than subscripts.
- do not change a variable's data class.
- use tic and toc to time parts of a program.
- use the profiler to check how long all parts of a function require.
Additional Tips on Improving Performance
If the performance of your program remains a concern, then consider the following suggestions:
- Split large script files into smaller ones, having the first file call the second if necessary.
- Construct separate functions (or local functions and nested functions) from the larger chunks of code.
- If you have overly complicated functions or expressions, use simpler ones to reduce size. * Simpler functions often make good utility functions that you can share with others.
- Use functions instead of scripts because they are generally faster.
- Vectorize your code to take advantage of the full functionality of MATLAB.
- Use a sparse matrix structure to vectorize code without requiring large amounts of memory.
- Avoid running large processes in the background while executing your program in MATLAB.
- Avoid overloading MATLAB built-in functions on any standard MATLAB data classes because this can negatively affect performance.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!