code is running too long, help me to optimize it

조회 수: 10 (최근 30일)
Asliddin Komilov
Asliddin Komilov 2019년 2월 26일
편집: Asliddin Komilov 2019년 2월 27일
I have written a code, it runs slow. I don't know if it is because of the code or just it should take so long because of the number of loops and calculations.
can you help me to check/improve it please.

채택된 답변

Jan
Jan 2019년 2월 26일
Start with inserting the code in a function, instead of using a script and try to use clear all to kill everything, which might be out of sight. This is a massive waste of time only.
Then mark the code with Ctrl-a and press Ctrl-i to apply a proper auto-indentation. This does not make the code faster, but much easier to debug. A lot of meaningful comments are essential also: Without it other readers cannot understand the purpose of the code- or you in some months. If I should work with this code, I'd give up immediately, because it is impossible to debug blocks like this:
Tc(jj) = Tc(jj-1)+dTc(jj-1);
Tt(jj)=Tt(jj-1)+dTt(jj-1);
Twout(jj)=Twout(jj-1)+dTwout(jj-1);
Tw(jj)=Tw(jj-1)+dTw(jj-1);
Tg1(jj) = Tg1(jj-1)+dTg1(jj-1);
Tadt(jj) = Tadt(jj-1)+dTadt(jj-1);
Tadb(jj) = Tadb(jj-1)+dTadb(jj-1);
t(jj)=t(jj-1)+1;
Ub(jj)=Ub(jj-1);
For the actual problem use the profiler to find the bottleneck: Which line needs the most processing time? It is not worth to optimze some code, which takes a few percent of the total time only.
It is not called often in your code, but remember that 10.^-5 is an expensive power operation, while 1e-5 is a cheap constant.
  댓글 수: 1
Asliddin Komilov
Asliddin Komilov 2019년 2월 27일
편집: Asliddin Komilov 2019년 2월 27일
Thank you very much, I will make it function after I complete it. I used the profiler as you have suggested. The main problem is the Xsteam function and lines like Ex(Ex<0)=0, could you share some ideas on this?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by