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

Chad Greene
Chad Greene 2015년 3월 21일
A good answer will require details. Give the simplest possible example of your code and maybe we can help.
Image Analyst
Image Analyst 2015년 3월 21일
Perhaps you mean iterations rather than increments. But that's a very small loop - it should take as short as a new nanoseconds, depending on what you're doing inside it. Of course, if there is a pause(5) inside the loop, it could take 5 seconds per iteration. What's inside your loop?
Stephen23
Stephen23 2015년 3월 21일
편집: Stephen23 2015년 3월 26일
@Sana Tayyeb: You don't give us any information at all about what code you are using, so how are we supposed to help you improve it? Sorry but we can't read your mind or your computer screen, so you actually need to give us some useful details about what you are using. Best of all you could upload your complete code using the paperclip button.
Sana Tayyeb
Sana Tayyeb 2015년 3월 22일
yes i meant iterations. Sorry for my mistake. see my code it reads from a file and download files using perl script and saves those files in an other file.
Image Analyst
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
Stephen23 2015년 3월 21일
편집: Stephen23 2015년 3월 21일

4 개 추천

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.
In the MATLAB documentation we also find these tips:
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.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 3월 21일

편집:

2015년 3월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by