Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
#include <stddef.h>
#include <inttypes.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define WAY_N 8U // number of ways
#define LINE_SIZE 64U // cache line size
#define LINE_N 64U // number of cache line
#define SPARSE WAY_N*LINE_SIZE*LINE_N
#define DENSE LINE_SIZE*LINE_N
#define AREA 29U*WAY_N*LINE_SIZE*LINE_N // allocated memory size
static uint8_t *p, *el, tmp;
void xchg (uint32_t slow_factor, uint32_t step) {
uint32_t i, j, k;
struct timeval start, end, diff;
gettimeofday(&start, NULL);
for (i = 0U, k = 0U, el = p; i < 200000000U/slow_factor; ++i) {
for (j = 0U; j < 7U*slow_factor; ++j, el += step, ++k) {
tmp = *el;
*el = *(el + step);
*(el + step) = tmp;
}
el = p;
}
gettimeofday(&end, NULL);
diff.tv_sec = end.tv_sec - start.tv_sec;
printf (" xchg() complited in %ld sec, k = %u.\n", diff.tv_sec, k);
}
int main(int argc, char const **argv) {
uint32_t i;
p = el = calloc (1, AREA);
for (i = 0U; i < AREA; ++i) {
*el++ = (uint8_t) rand();
}
xchg (1U, SPARSE);
xchg (1U, DENSE);
xchg (4U, SPARSE);
xchg (4U, DENSE);
return 0;
}
Это про кеш L1, L2 или L3?
Логическая организация кэш-памяти процессора