Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
if A&&B else optimization and profile question

Subject: if A&&B else optimization and profile question

From: Louis

Date: 15 Oct, 2010 13:51:03

Message: 1 of 9

Hello all,

My code iterates an if-else statement for literally hundreds of thousands of times. Also I try to optimize my code to perform in real-time.

I checked the profile reviewer report and it says that I have some time consumption in such thing:

if A && B
 do
else do another thing

I am thinking of splitting the if A &&B as it seems redundant and I can do it in two steps such as
if A
  if B
     do
else do another thing

I don’t seem to succeed with this :(, can you please advise me how to do it?

Do you have an idea if I can use other logical operation that fasten this if statement ? such as in the comparison?

Also i have another question about profile reviewer, it says that i have much of the time spent in the "end" statement of a for loop! What does this mean?

Many thanks in advance

Subject: if A&&B else optimization and profile question

From: Cris Luengo

Date: 15 Oct, 2010 14:59:04

Message: 2 of 9

"Louis " <wlouis@ryerson.ca> wrote in message <i99m87$brn$1@fred.mathworks.com>...
> Hello all,
>
> My code iterates an if-else statement for literally hundreds of thousands of times. Also I try to optimize my code to perform in real-time.
>
> I checked the profile reviewer report and it says that I have some time consumption in such thing:
>
> if A && B
> do
> else do another thing
>
> I am thinking of splitting the if A &&B as it seems redundant and I can do it in two steps such as
> if A
> if B
> do
> else do another thing
>
> I don’t seem to succeed with this :(, can you please advise me how to do it?
>
> Do you have an idea if I can use other logical operation that fasten this if statement ? such as in the comparison?

If I'm not mistaken, MATLAB implements the && operator as a shortcut logical operation. That is, if the left operand is false, the right one is never evaluated. So splitting it up into two separate if statements doesn't improve the speed. You are more likely to improve speed by removing the loop around this if construct. If you post your loop here, the community might be able to help you speed it up.

> Also i have another question about profile reviewer, it says that i have much of the time spent in the "end" statement of a for loop! What does this mean?

During the end statement of a for loop, there's the check to see if control needs to go back to the start of the loop or not. The index increment is possibly also counted at the end statement. Don't worry too much what each individual statement does, but look at execution speed of sections of code. That will tell you which parts of the code need rewriting or turning into a MEX file.

Cheers,
Cris.

Subject: if A&&B else optimization and profile question

From: Jan Simon

Date: 15 Oct, 2010 15:22:06

Message: 3 of 9

Dear Louis,

don't let the profiler confuse you.
Neither the && nor END can or must be improved.
Remember that the JIT-acceleration combines and reorders commands and the profiler measures how much time is spent in the specific "lines".

Example:
  x = rand(100,1);
  y = rand(100,100);
  for i = 1:10
    k = y + x(:, ones(1, 100));
  end

Under some circumstances in this example *no* time is spent inside the ONES function, because the JIT "understands" that the explicitely expanded x is not needed at all.

Example:
  for i = 1:100000
  end
Nothing happens, but the profile has to decide where the time is spent. And the increase of the loop counter happens inside "end", therefore the profiler claims that "end" is the slowest line of the program.

I'd suggest to post the code and hope that anybody has some tricky ideas.
- Hey, it's really me. I've told so many times "better ask your profiler than the newgroup". Why not try it the other way around.

Jan

Subject: if A&&B else optimization and profile question

From: Louis

Date: 15 Oct, 2010 15:31:04

Message: 4 of 9

Thank you so much Cris for your help. I am trying to vectorize the for-loop as advised, i hope it can be vectorized.

I am encountering the follwoing problem now (which is a mini version of my code):

A=[1 2 3 4 5;
     6 7 8 9 10;
    11 12 13 14 15;
    16 17 18 19 20];

 x=[1;2;3]; % some desired 'x' index of matrix A
 y=[2;4;4]; % some desired 'y' index of matrix A

% instead of performing the if statement inside a for-loop i vectorized it
ANDED=[1;0;1];

% so instead of the if-else statement i want to do the follwoing:
% when ANDED=1, take the pixel of A(x,y) and do the operation and when
% ANDED=0, take the corresponding A(x,y) and do another operation

% so this is what i am doing but is not working:
MATRIX=A(y(ANDED==1),x(ANDED==1))

MATRIX =

          6.00 8.00
         16.00 18.00
         
%but what i want is A(2,1), and A(4,3) which correspond to ANDED==1:
MATRIX= 6
             18


Then i can do the same when ANDED==0 :)

Thank you very much for your hlep, and i hope it is clear....

ps. i will MEX it as advised but i havent mexed a file before so i am still reading on how to do it:)

Subject: if A&&B else optimization and profile question

From: Louis

Date: 15 Oct, 2010 15:34:04

Message: 5 of 9

Thank you very much Jan. I posted a mini-version of the operation :)

Thank you guys for your help


Louis

Subject: if A&&B else optimization and profile question

From: Louis

Date: 15 Oct, 2010 16:11:04

Message: 6 of 9

"Louis " <wlouis@ryerson.ca> wrote in message <i99s9c$q75$1@fred.mathworks.com>...
> Thank you very much Jan. I posted a mini-version of the operation :)
>
> Thank you guys for your help
>
>
> Louis

I did it this way :)

N(ANDED==1)=A(sub2ind(size(A),y(ANDED==1),x(ANDED==1)))

and it seems to work :)

Thank you guys again

Subject: if A&&B else optimization and profile question

From: Louis

Date: 15 Oct, 2010 17:12:04

Message: 7 of 9

> N(ANDED==1)=A(sub2ind(size(A),y(ANDED==1),x(ANDED==1)))


Hello again guys,

I thought my problem is solved however the above method using sub2ind is time consuming. It costed me 10 sec :S

Is there any alternative to solve the matrix issue

Regards,

Louis

Subject: if A&&B else optimization and profile question

From: Louis

Date: 15 Oct, 2010 17:52:03

Message: 8 of 9

Here is a bigger bunch of the code and that shows how many times sub2ind i am using. Based on the profile report, each line is iterated for about 59000 times. also A in my case is a matrix of size 24x24...

Hence using this code, these sub2ind lines take around 10 seconds :S:S

Any help in optimizing this code is highly appreciated

Louis

Subject: if A&&B else optimization and profile question

From: Louis

Date: 15 Oct, 2010 17:58:04

Message: 9 of 9

Sorry i forgot the code

A=[1 2 3 4 5;
   6 7 8 9 10;
   11 12 13 14 15;
   16 17 18 19 20];

x=[1;2;3]; % some desired 'x' index of matrix A
y=[2;4;4]; % some desired 'y' index of matrix A

fy = floor(y);
cy = ceil(y);
rx = round(x);
ry = round(y);
fx = floor(x);
cx = ceil(x);

ANDED=[1;0;1]; % instead of performing the if statement inside a for-loop i vectorized

% so instead of the if-else statement i want to do the follwoing:
% whended ANDED=1, take the pixel of A(x,y) and do the operation and when
% ANDED=0, take the corresponding A(x,y)


N(ANDED==1)=A(sub2ind(size(A),ry(ANDED==1),rx(ANDED==1)));
    
     
N(ANDED==0) = A(sub2ind(size(A),fy(ANDED==0),fx(ANDED==0))) + ...
               A(sub2ind(size(A),fy(ANDED==0),cx(ANDED==0))) + ...
               A(sub2ind(size(A),cy(ANDED==0),fx(ANDED==0))) + ...
               A(sub2ind(size(A),cy(ANDED==0),cx(ANDED==0)));
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us