Cache Memory
Why is Cache Local Memory Important?
When you access memory, a chunks of it are cached at various levels (L1, L2, L3). Cache locality refers to the likelihood of successive operations being in the cache and thus being faster. In an array, you maximize the chances of sequential element access being in the cache.
Being able to look at code and get a qualitative sense of its locality is a key skill for a professional programmer.
1-D Array Example
#include <stdio.h>
int sum_1darray(int a[], int size) {
int sum = 0;
int i;
for (i = 0; i < size; ++i) {
printf("%p: a[%d] = %d\n", &a[i], i, a[i]);
sum += a[i];
}
return sum;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = sum_1darray(arr, 5);
printf("sum = %d\n", sum);
return 0;
}
Output:
0x16d43f280: a[0] = 1
0x16d43f284: a[1] = 2
0x16d43f288: a[2] = 3
0x16d43f28c: a[3] = 4
0x16d43f290: a[4] = 5
sum = 15
The 1-D Array
is read sequentially in-order since its a 1-D Array
. We can see a table of this below:
Caching in the Memory Hierarchy
Cache: Small, Fast storage device that acts as a staging area for the data objects stored in a larger and slower device. The process of using a cache is called caching.
Local Disk: Cache for webpages retrieved from Web Servers.
Main Memory: Cache for Data on the local disks of computer.
Cache Memories: Cache for data on the Main Memory.
CPU Registers: Cache for data on the CPU Caches.