I teach a university course on numerical methods and MATLAB. I assigned my students to evaluate the execution times for various operations. Specifically, I had them compare the following two cases:
format short tic for i=1:1000 for j=1:1000 for k=1:1000 x=k; end end end toc
versus this:
format short tic for i=1:1000 for j=1:1000 for k=1:1000 x=j+k; end end end toc
The second code with the added addition operation reproducibly takes significantly (~30%) less execution time than the first. This seemingly implausible result appears to occur in version R2011b and not earlier versions (although I haven't done an exhaustive test). Any explanation for this? (BTW, the same result holds if the addition is replaced by a multiplication).
Best Answer