1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#include <unistd.h>

/* return real time in seconds since start of the process */
double walltime() {
   static clock_t ticks_per_second = 0;
   if (!ticks_per_second) {
      ticks_per_second = sysconf(_SC_CLK_TCK);
   }
   struct tms timebuf;
   /* times returns the number of real time ticks passed since start */
   return (double) times(&timebuf) / ticks_per_second;
}

void init_vector(double* v, size_t len, ptrdiff_t incr) {
   for (size_t i = 0; i < len; ++i) {
      v[i*incr] = i + 1;
   }
}

void print_vector(double* v, size_t len, ptrdiff_t incr) {
   for (size_t i = 0; i < len; ++i) {
      printf(" %4.1lf", v[i*incr]);
   }
   printf("\n");
}

#define MAX_LEN 134217728
#define MAX_RUNS 1024

int main() {
   printf("     len t1 (separate) t2 (interleaving)      t2/t1\n");
   double* vector1[MAX_RUNS]; double* vector2[MAX_RUNS];
   size_t runs = MAX_RUNS;
   for (size_t len = 8192; len <= MAX_LEN/2; len *= 2) {
      printf("%8zu", len);

      for (size_t i = 0; i < runs; ++i) {
	 vector1[i] = malloc(sizeof(double) * len * 2);
	 vector2[i] = malloc(sizeof(double) * len * 2);
      }
      /* separate vectors */
      double t0 = walltime();
      for (size_t i = 0; i < runs; ++i) {
	 double* x = vector1[i];
	 double* y = vector1[i] + len;
	 init_vector(x, len, 1);
	 init_vector(y, len, 1);
      }
      double t1 = (walltime() - t0) / runs;
      printf(" %12.6lf", t1);

      /* interleaved vectors */
      t0 = walltime();
      for (size_t i = 0; i < runs; ++i) {
	 double* x = vector2[i];
	 double* y = vector2[i] + 1;
	 init_vector(x, len, 2);
	 init_vector(y, len, 2);
      }
      double t2 = (walltime() - t0) / runs;
      printf(" %12.6lf %16.6lf", t2, t2/t1);
      printf("\n");

      for (size_t i = 0; i < runs; ++i) {
	 free(vector1[i]); free(vector2[i]);
      }
      if (runs > 1) runs /= 2;
   }
}