Put small intermediate results from forLoops in temporary variables

Put small intermediate results from forLoops in temporary variables that are  copied and update large intermediate results in-place.

No functional change. Gives 2-3% better performance on matrix multiplication.