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 statements - drastic difference in running time

Subject: if statements - drastic difference in running time

From: MaxEOE

Date: 22 Mar, 2012 12:46:11

Message: 1 of 3

Hi,
I have encountered the following odd behavior I have no explanation for:
In the following code snippet I observe dramatically different running times, depending on the usage of the if statements, which both give the same results. The if query 2 takes about ten times longer than the if query 2.
Has anyone an explanation for this? I run the code on Matlab 7.8.0.347 (R2009a).

tic;

r_array = rand(10,1);
for i=1:10
    r1 = r_array(i);
    r2 = rand(1);
    T = 0;
    for j=1:1000000
        T = T + 0.000001;
         if r1<T % if query 1
% if r2<T % if query 2
             display(i);
             break
         end
    end
end

toc;

Thank you,
MaxEOE

Subject: if statements - drastic difference in running time

From: Chad Gilbert

Date: 22 Mar, 2012 14:43:12

Message: 2 of 3

"MaxEOE" wrote in message <jkf6uj$ni$1@newscl01ah.mathworks.com>...
> Hi,
> I have encountered the following odd behavior I have no explanation for:
> In the following code snippet I observe dramatically different running times, depending on the usage of the if statements, which both give the same results. The if query 2 takes about ten times longer than the if query 2.
> Has anyone an explanation for this? I run the code on Matlab 7.8.0.347 (R2009a).
>
> tic;
>
> r_array = rand(10,1);
> for i=1:10
> r1 = r_array(i);
> r2 = rand(1);
> T = 0;
> for j=1:1000000
> T = T + 0.000001;
> if r1<T % if query 1
> % if r2<T % if query 2
> display(i);
> break
> end
> end
> end
>
> toc;
>
> Thank you,
> MaxEOE

I'm stuck guessing here. But I assume you mean to say that it is ~10 times longer to use if #1 than if #2. If you were a super-smart MATHWORKS guy, and you were designing an interpreted language, it might be nice if you could notice when the same statement is getting evaluated over and over again. When that happened, you could just cache the result and keep re-applying it, without having to (expensively!) re-evaluate the same statement.

Now. look at your second "if" statement. I could cache the result once, and then use it all 10*1000000 times in your set of nested loops. But for "if" statement #2, I'd have to re-compute that value every time the value of r1 changed. That happens - you guessed it - ten times. This is why your first script takes ten times longer to execute than the second - the if statement is computed ten times, instead of one time*.

*(I'm lying a bit here, but that gives the gist. I'd love to see someone give a more detailed explanation as to *how* MATLAB caches these results, and *how* it recognizes when a statement needs to be re-evaluated.)

Subject: if statements - drastic difference in running time

From: Roger Stafford

Date: 23 Mar, 2012 02:18:13

Message: 3 of 3

"MaxEOE" wrote in message <jkf6uj$ni$1@newscl01ah.mathworks.com>...
> I have encountered the following odd behavior I have no explanation for:
> ........
- - - - - - - - - -
  You should be collecting the sum of the ten j-values at the point where the 'break' occurs, which will give you the total number of trips through the inner loop. On my machine when I take the ratio of that sum to the tic-toc time it varies very little from one run to another and between query 1 and 2. Try this on your machine.

  Remember, 10 is not a very large number and there should be considerable variation in the above sum, which might conceivably account for your observations. If not, then you would have a much more convincing case to present to MathWorks' experts.

Roger Stafford

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