21 views (last 30 days)

I tried the following with two options - matrix and scalar

clear;

N = 1e7;

tic

for n = 1:N

% a=[n n;n n]/2;

a = n/2;

b = a/2;

end

toc

clear;

N=1e7;

tic

for k=1:N

% c=[k k;k k]*0.5;

c=k*0.5;

d=c*0.5;

end

toc

clear;

N=1e7;

tic

for m=1:N

%f=[m m;m m]/1.41;

f=m/1.41;

g=f/1.41;

end

toc

clear;

N=1e7;

tic

for t=1:N

% p=[t t;t t]*1.41;

p=t*1.41;

q=p*1.41;

end

toc

And after the second run (the first run is similar) I got the following results. For a matrix:

Elapsed time is 10.591345 seconds.

Elapsed time is 12.099287 seconds.

A*0.5 is slightly slower than A/2

Elapsed time is 12.162518 seconds.

Elapsed time is 10.652158 seconds.

A*1.41 is slightly faster than A/1.41.

For a scalar:

Elapsed time is 0.024380 seconds.

Elapsed time is 0.017146 seconds.

almost no difference between A/2 and A*0.5

Elapsed time is 0.150341 seconds.

Elapsed time is 0.017156 seconds.

finally, A*1.41 is sufficiently faster than A/1.41.

I was taught that multiplication is always much faster than division. Could you please explain me how this works in Matlab?

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/482212-multiplication-by-0-5-vs-division-by-2#comment_750766

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/482212-multiplication-by-0-5-vs-division-by-2#comment_750766

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/482212-multiplication-by-0-5-vs-division-by-2#comment_750872

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/482212-multiplication-by-0-5-vs-division-by-2#comment_750872

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/482212-multiplication-by-0-5-vs-division-by-2#comment_750925

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/482212-multiplication-by-0-5-vs-division-by-2#comment_750925

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/482212-multiplication-by-0-5-vs-division-by-2#comment_750967

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/482212-multiplication-by-0-5-vs-division-by-2#comment_750967

Sign in to comment.