MATLAB: Are there timing differences in the computation of equivalent matrix multiplication scenarios

computationMATLABmatrixmultiplicationtime

I am writing programs that are very speed sensitive. Why are there timing differences in the computation of the following equivalent matrix multiplication scenarios? The scenarios are:
1. C - C*A*A'
2. C - C * B % where B is pre-computed as A*A'
3. C * (I - D) % where D is pre-computed as A * B
In the following example:
>> N=4;
>> M=10^N;
>> A=randn(200,6);
>> B=A*A';
>> C=randn((16*50),200);
>> D=eye(200)-B;
>> E=randn((16*50),200);
>> F=randn((16*50),200);
>> G=randn((16*50),200);
>> tic;for(ii=1:M),E=C-C*A*A';end;toc/M,tic;for(ii=1:M),F=C-C*B;end;toc/M,tic;for(ii=1:M),G=C*D;end;toc/M
The timing results are:
0.00052
0.00098
0.00085
As you notice, the time required for the second and third scenarios are approximately twice as that by the first scenario.

Best Answer

The timing difference is related to the amount of flops computed and the order of the multiplications. This is because "A" is not square but is 200x6 (so B is a rank-6 matrix).
In the first case, the operation, C – C*A *A’, computed two matrix multiplications and a subtraction, so roughly the following number of flops are computed:
800*200 + 800*200*6 + 800*6*200 flops, so around 2 million.
For the second and the third case the number of flops computed are as follows:
800*200 + 200*6*200 + 800*200*200 flops, so around 32 million.
As the amount of flops computed in the second and the third case is larger than the first case, the timing results show that the time required for these operations are more. If you perform the same tests, but time the results for the following operation:
>> C C*(A*A)
You will see similar timing results as the second and third cases.