$A$ and $B$ are $n \times n$ matrices and $v$ is a vector with $n$ elements. $Av$ has $\approx 2n^2$ flops and $A+B$ has $n^2$ flops. Following this logic, $(A+B)v$ should be faster than $Av+Bv$.
Yet, when I run the following code in matlab
```A = rand(2000,2000);
B = rand(2000,2000);
v = rand(2000,1);
tic
D=zeros(size(A));
D = A;
for i =1:100
    D = A + B;
    (D)*v;
end
toc
tic
for i =1:100
    (A*v+B*v);
end
toc
```
The opposite is true. Av+Bv is over twice as fast. Any explanations?
It is likely that you are experiencing the cache locality effects in your code. When you perform matrix-vector multiplication $Av$ or $(A+B)v$, the data in matrix $A$ and vector $v$ are loaded into the cache multiple times during the computation. This leads to a higher cache hit rate and faster computation.

When you compute $Av+Bv$, Matlab performs the element-wise addition and multiplication operations element by element. For large
It is likely that Matlab is performing some matrix multiplication optimizations internally, which can make the matrix addition and multiplication with a vector computation faster when performed in a single expression (i.e., `(A+B)*v`) rather than separate steps (i.e., `A*v+B*v`). You can try using the `mldivide` function for matrix-vector multiplication to see if it makes a difference:
```sc