From: Binlin Wu on
If you have enough memory, do the following.

a = ones(10000,1000);

Then do something like

Then compare

Case 1:
for i = 1:10
b = sqrt(a.^2);
end

Case 2:

b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);% run in one command line

Case 3:
b = sqrt(a.^2);
b = sqrt(a.^2);
b = sqrt(a.^2);
b = sqrt(a.^2);
b = sqrt(a.^2);
b = sqrt(a.^2);
b = sqrt(a.^2);
b = sqrt(a.^2);
b = sqrt(a.^2);
b = sqrt(a.^2);
% run in separate command lines

You’ll get 1st and 2nd case run almost with the same speed (maybe different, I can’t tell significant difference.) But 3rd case will be much faster.

Why?
From: Binlin Wu on
Sorry. It's a mistake.
I want to close this post.

"Binlin Wu" <cunyalen(a)gmail.com> wrote in message <hp3jg9$f54$1(a)fred.mathworks.com>...
> If you have enough memory, do the following.
>
> a = ones(10000,1000);
>
> Then do something like
>
> Then compare
>
> Case 1:
> for i = 1:10
> b = sqrt(a.^2);
> end
>
> Case 2:
>
> b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);b = sqrt(a.^2);% run in one command line
>
> Case 3:
> b = sqrt(a.^2);
> b = sqrt(a.^2);
> b = sqrt(a.^2);
> b = sqrt(a.^2);
> b = sqrt(a.^2);
> b = sqrt(a.^2);
> b = sqrt(a.^2);
> b = sqrt(a.^2);
> b = sqrt(a.^2);
> b = sqrt(a.^2);
> % run in separate command lines
>
> You&#8217;ll get 1st and 2nd case run almost with the same speed (maybe different, I can&#8217;t tell significant difference.) But 3rd case will be much faster.
>
> Why?