Advertisement
karlicoss

cache line

Aug 24th, 2014
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int SIZE = 1024 * 1024 * 1024;
  6. int CLSIZE = 64;
  7.  
  8. void test(char *a, int step) {
  9.     clock_t start = clock();   
  10.  
  11.     for (int i = 0; i < SIZE; i += step) {
  12.         a[i] *= 2;
  13.     }
  14.  
  15.     clock_t end = clock();
  16.     float ms = (float)(end - start) / (CLOCKS_PER_SEC / 1000); 
  17.     printf("Step = %d: %f ms\n", step, ms);
  18. }
  19.  
  20. void main() {
  21.     char *aaa = (char *)malloc(SIZE);
  22.     test(aaa, 4);
  23.     test(aaa, 64);
  24. }
  25.  
  26. // $ gcc cache_line.c -std=c99 -O0 -o cache_line && ./cache_line
  27. // Step = 4: 813.681030 ms
  28. // Step = 64: 110.019997 ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement