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?

### Like this:

Like Loading...

*Related*

Filed under: Matlab

cunyalen1, on April 2, 2010 at 2:24 am said:Sorry. It’s a mistake.