Play "Processor Cache Gallery"

After reading this article, I tried to reproduce the example on my Mac. However, my curve for the first example looks completely different, and I don’t understand why ..

Updating every k-th int

My code is below:

#include <mach/mach_time.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

void mach_absolute_difference(uint64_t end, uint64_t start, struct timespec *tp) {
    uint64_t difference = end - start;
    static mach_timebase_info_data_t info = {0,0};

    if (info.denom == 0)
        mach_timebase_info(&info);

    uint64_t elapsednano = difference * (info.numer / info.denom);

    tp->tv_sec = elapsednano * 1e-9;
    tp->tv_nsec = elapsednano - (tp->tv_sec * 1e9);
}

int main(void)
{
    int len = 64 * 1024 * 1024;
    int *arr = (int *)malloc(sizeof(int)*len);

    uint64_t start,end;
    struct timespec tp;

    start = mach_absolute_time();
    for (int i = 0; i <len; i += K) 
        arr[i] = 0;
    end = mach_absolute_time();

    mach_absolute_difference(end, start, &tp);

    FILE *fp;
    fp=fopen("simple_array.log", "a+");
    fprintf(fp, "%i\t%ld\t%ld\n", K, tp.tv_sec,
            tp.tv_nsec);
    fclose(fp);

    free(arr);
    return 0;
}

I measured the time described in this blog , hoping it would be correct. I am also wondering what should I use to measure runtime or CPU cycles on a Mac. Or it would even be nicer to see the number of hits / skips of the cache for a particular function. However, Shark only shows percentages of l2 cache misses.

Update this when compiling for 32-bit, note that the size of int varies from 8 to 4 bytes

Updating every k-th int compiled for 32bit

+3
1

:

  • ; , ?
  • int, ,
  • K, (i len )
+1

Source: https://habr.com/ru/post/1768764/


All Articles