http://www.mathworks.com/matlabcentral/newsreader/view_thread/318235
MATLAB Central Newsreader  if statements  drastic difference in running time
Feed for thread: if statements  drastic difference in running time
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 22 Mar 2012 12:46:11 +0000
if statements  drastic difference in running time
http://www.mathworks.com/matlabcentral/newsreader/view_thread/318235#870835
MaxEOE
Hi,<br>
I have encountered the following odd behavior I have no explanation for:<br>
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.<br>
Has anyone an explanation for this? I run the code on Matlab 7.8.0.347 (R2009a).<br>
<br>
tic;<br>
<br>
r_array = rand(10,1);<br>
for i=1:10<br>
r1 = r_array(i);<br>
r2 = rand(1);<br>
T = 0;<br>
for j=1:1000000<br>
T = T + 0.000001;<br>
if r1<T % if query 1<br>
% if r2<T % if query 2<br>
display(i);<br>
break<br>
end<br>
end<br>
end<br>
<br>
toc;<br>
<br>
Thank you,<br>
MaxEOE

Thu, 22 Mar 2012 14:43:12 +0000
Re: if statements  drastic difference in running time
http://www.mathworks.com/matlabcentral/newsreader/view_thread/318235#870852
Chad Gilbert
"MaxEOE" wrote in message <jkf6uj$ni$1@newscl01ah.mathworks.com>...<br>
> Hi,<br>
> I have encountered the following odd behavior I have no explanation for:<br>
> 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.<br>
> Has anyone an explanation for this? I run the code on Matlab 7.8.0.347 (R2009a).<br>
> <br>
> tic;<br>
> <br>
> r_array = rand(10,1);<br>
> for i=1:10<br>
> r1 = r_array(i);<br>
> r2 = rand(1);<br>
> T = 0;<br>
> for j=1:1000000<br>
> T = T + 0.000001;<br>
> if r1<T % if query 1<br>
> % if r2<T % if query 2<br>
> display(i);<br>
> break<br>
> end<br>
> end<br>
> end<br>
> <br>
> toc;<br>
> <br>
> Thank you,<br>
> MaxEOE<br>
<br>
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 supersmart 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 reapplying it, without having to (expensively!) reevaluate the same statement.<br>
<br>
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 recompute 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*.<br>
<br>
*(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 reevaluated.)

Fri, 23 Mar 2012 02:18:13 +0000
Re: if statements  drastic difference in running time
http://www.mathworks.com/matlabcentral/newsreader/view_thread/318235#870905
Roger Stafford
"MaxEOE" wrote in message <jkf6uj$ni$1@newscl01ah.mathworks.com>...<br>
> I have encountered the following odd behavior I have no explanation for:<br>
> ........<br>
         <br>
You should be collecting the sum of the ten jvalues 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 tictoc time it varies very little from one run to another and between query 1 and 2. Try this on your machine.<br>
<br>
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.<br>
<br>
Roger Stafford