rendered paste body/* * 2D arrays - pointer implementations */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/time.h>#include <time.h>typedef struct timeval tv;static inline doubletm(tv *start, tv *finish){ double tdiff, udiff, result; if (!start || !finish) { fprintf(stderr, "[tm] NULL pointer passed!\n"); exit(1); } tdiff = (double) (finish->tv_sec - start->tv_sec); udiff = (double) (finish->tv_usec - start->tv_usec); result = tdiff + udiff / 1000000000; return result;}static inline voidprintt(tv *start, tv *finish, const char *msg){ printf("%s %.5f seconds\n", msg, tm(start, finish));}int main (int argc, char **argv){ double **a, **b; long rows = 1024, cols = 1024, i, j; tv *beg = NULL, *end = NULL; beg = (tv*) malloc(sizeof(tv)); end = (tv*) malloc(sizeof(tv)); if (argc > 1) { rows = strtol(argv[1], NULL, 10); if (argc > 2) cols = strtol(argv[2], NULL, 10); } /* continuous allocation */ a = (double**) malloc(rows * sizeof(double*) + rows * cols * sizeof(double)); for (i = 0; i < rows; i++) a[i] = &a[rows + i*cols]; /* non-continuous allocation */ b = (double**) malloc(i * sizeof(double*)); for (i = 0; i < rows; i++) b[i] = (double*) malloc(cols * sizeof(double)); /* speed tests */ gettimeofday(beg, NULL); for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) a[i][j] = 1.0; gettimeofday(end, NULL); printt(beg, end, "a init"); gettimeofday(beg, NULL); for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) b[i][j] = 1.0; gettimeofday(end, NULL); printt(beg, end, "b init"); free(beg); free(end); return 0;}