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
     74
     75
     76
     77
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#include <unistd.h>

#define MAXDIM_M    5000
#define MAXDIM_N    5000

double buffer[MAXDIM_M*MAXDIM_N];

double
walltime()
{
   struct tms    ts;
   static double ClockTick=0.0;

   if (ClockTick==0.0) {
        ClockTick = 1.0 / ((double) sysconf(_SC_CLK_TCK));
   }
   return ((double) times(&ts)) * ClockTick;
}

void
initMatrix(long m, long n, double *A, long incRowA, long incColA)
{
    long i, j;

    for (j=0; j<n; ++j) {
        for (i=0; i<m; ++i) {
            A[i*incRowA+j*incColA] = j*n+i+1;
        }
    }
}

void
printMatrix(long m, long n, const double *A, long incRowA, long incColA)
{
    long i, j;

    for (i=0; i<m; ++i) {
        printf("   ");
        for (j=0; j<n; ++j) {
            printf("%4.1lf ", A[i*incRowA+j*incColA]);
        }
        printf("\n");
    }
    printf("\n");
}

int
main()
{
    long    m, n;
    double  t0, t1, t2;

    printf("   M    N     t1 (colMajor)  t2 (rowMajor)      t2/t1\n");
    printf("=====================================================\n");
    for (m=128, n=128; m<=MAXDIM_M && n<MAXDIM_N; m+=128,n+=128) {

        printf("%4ld %4ld ", m, n);

        t0 = walltime();
        initMatrix(m, n, buffer, 1, m);
        t1 = walltime() - t0;

        printf(" %12.2lf", t1);

        t0 = walltime();
        initMatrix(m, n, buffer, n, 1);
        t2 = walltime() - t0;

        printf(" %12.2lf %16.2lf", t2, t2/t1);
        printf("\n");
    }

    return 0;
}