Less time consuming substitution for if conditional

조회 수: 3 (최근 30일)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019년 9월 29일
답변: Walter Roberson 2019년 9월 30일
I have a long script with lots of if conditional and it takes a long time to run the code. Any suggestion to make it efficient for example a less time consuming substitution for if conditional.
for example:
if rim==core
rim=rim+1;
end
Can I change above if conditional to shorter script?

답변 (2개)

John D'Errico
John D'Errico 2019년 9월 29일
I think you are focusing on the wrong things. This is NOT a slow operation.
Very likely, this is part of a large, inefficiently written code, where you are doing tests like that for the wrong reason. But we don't see your code.
So the first thing you should do is learn to use the profiler. It will tell you what lines of code are taking a lot of time. Then focus on them.
As importantly though, my guess is as I said, that your entire code is written inefficiently. You need to learn to use vectorized operations, where operations will apply to an entire set of values at once. That is how you will find significant speed boosts, NOT by hoping to save a hundreth of a millisecond on a simple test like that.
  댓글 수: 1
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019년 9월 30일
@ John D'Errico: I am doing vectorized operations like varfun that works on etire set but I need to search specific criteria in each parameter of the set. I donot think there is a faster speed: No matter the design of the code, it needs to check each member of the vector for specific property.

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


Walter Roberson
Walter Roberson 2019년 9월 30일
rim(rim == core) = core+1;

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by