A set of variables has temporal locality if they are accessed within a relatively short period of time. sum
, k
and n
are all accessed at every iteration of the inner loop; assuming that n
is relatively large, i
and j
are not accessed frequently. So sum
, k
and n
are temporally local, j
could be considered temporally local with them but i
probably isn't.
Variables have spatial locality if they are stored at addresses that are close together – e.g., close enough to be in the same cache line or, at least, the same page in memory. As such, spatial locality is a property of compiled code, not source code, so it's impossible to say what variables in the code are spatially local: a stupid compiler or memory manager could scatter them to the four corners of the address space. But one would hope that temporally local variables that are accessed repeatedly would be spatially local.
i
fit neither? $\endgroup$