// Stride testi: 1 GB massivdə hər k-cı elementə toxunmaq.
// k = 1..16 olanda toxunulan cache line sayı eynidir (hamısı), ona görə vaxt az dəyişir.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static double now(void) {
    struct timespec t;
    clock_gettime(CLOCK_MONOTONIC, &t);
    return t.tv_sec + t.tv_nsec / 1e9;
}

int main(void) {
    size_t n = (size_t)1 << 28;                  // 2^28 int = 1 GB
    int *a = malloc(n * sizeof(int));
    for (size_t i = 0; i < n; i++) a[i] = 1;
    for (size_t k = 1; k <= 256; k *= 2) {
        double best = 1e9;
        for (int r = 0; r < 3; r++) {
            double t0 = now();
            for (size_t i = 0; i < n; i += k) a[i] *= 3;
            double t = now() - t0;
            if (t < best) best = t;
        }
        size_t touches = n / k;
        size_t lines = k <= 16 ? n / 16 : touches;   // toxunulan cache line sayı
        printf("k=%3zu (%4zu byte)  toxunus: %9zu  line: %9zu  vaxt: %6.1f ms  bir toxunus: %5.2f ns\n",
               k, k * 4, touches, lines, best * 1e3, best / touches * 1e9);
    }
    free(a);
    return 0;
}
