multiplication by 0.5 vs division by 2
17 views (last 30 days)
I tried the following with two options - matrix and scalar
N = 1e7;
for n = 1:N
% a=[n n;n n]/2;
a = n/2;
b = a/2;
% c=[k k;k k]*0.5;
%f=[m m;m m]/1.41;
% p=[t t;t t]*1.41;
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?