COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### if A&&B else optimization and profile question

• Follow

```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&#8217;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?

```
 0

```"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&#8217;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.
```
 0

```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
```
 0

```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:)
```
 0

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

Thank you guys for your help

Louis
```
 0

```"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
```
 0

```> 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
```
 0

```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
```
 0

```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)));

```
 0

8 Replies
332 Views

5/29/2013 11:08:15 AM