#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; } } |