School of Computing and Information Systems
COMP30023: Computer Systems
Practical Week 4
Copyright By PowCoder代写 加微信 powcoder
1 Spatial & Temporal Locality
The principle of spatial locality says that if a program accesses one memory address, there is
a good chance that it will also access other nearby addresses. CPU caches take advantage of
this by reading in cache lines from physical memory and reading from the CPU cache is order
of magnitudes faster than actually reading from Physical RAM1.
Write two programs, fast.c and slow.c, which access or modify a two dimensional array in
a way that is fast (exhibits spatial locality) and slow (does not exhibit spatial locality) respec-
Ensure that fast takes at least 0.3 seconds to run.
You may want to use the time command, e.g. $ time ./slow , to measure the time it takes
for a program to finish.
The principle of temporal locality states says that if a program accesses one memory address,
there is a good chance it will access the same memory address again.
Write another pair of programs, fast-temp.c and slow-temp.c, which access or modify a one
dimensional array in a way that is fast (exhibits temporal locality) and slow respectively.
Sample solutions will be later released.
1https://gist.github.com/jboner/2841832
#define BIG 10000
int big[BIG][BIG];
int main(void) {
for (int i = 0; i < BIG; i++) { for (int j = 0; j < BIG; j++) { big[i][j] = 0; #define BIG 10000 int big[BIG][BIG]; int main(void) { for (int i = 0; i < BIG; i++) { for (int j = 0; j < BIG; j++) { big[j][i] = 0; C fast-temp.c int arr[16777216]; int main(void) { unsigned long long cur; int index[] = {1, 25, 168, 1229, 9592, 78498, 128, 2048, 65536, 99999}; for (long iter = 0; iter < 10000000; iter++) { for (int i = 0; i < 10; i++) { cur = (index[i] * 8121 + 28411) % 16777216; arr[cur] += 1; D slow-temp.c int arr[16777216]; int main(void) { unsigned long long cur = 0; for (long iter = 0; iter < 10000000; iter++) { for (int i = 0; i < 10; i++) { cur = (cur * 8121 + 28411) % 16777216; arr[cur] += 1; 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com