language: C++ 4.7.2 (gcc-4.7.2)
date: 255 days 9 hours ago
link:
visibility: public
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
78
79
double vectors_dot_prod(const double *x, const double *y, int n)
{
    double res = 0.0;
    int i;
    for (i = 0; i < n; i++)
    {
        res += x[i] * y[i];
    }
    return res;
}
 
void matrix_vector_mult(const double **mat, const double *vec,
                        double *result, int rows, int cols)
{ // in matrix form: result = mat * vec;
    int i;
    for (i = 0; i < rows; i++)
    {
        result[i] = vectors_dot_prod(mat[i], vec, cols);
    }
}
 
double vectors_dot_prod2(const double *x, const double *y, int n)
{
    double res = 0.0;
    int i = 0;
    for (; i <= n-4; i+=4)
    {
        res += (x[i] * y[i] +
                x[i+1] * y[i+1] +
                x[i+2] * y[i+2] +
                x[i+3] * y[i+3]);
    }
    for (; i < n; i++)
    {
        res += x[i] * y[i];
    }
    return res;
}
 
void matrix_vector_mult2(const double **mat, const double *vec,
                         double *result, int rows, int cols)
{ // in matrix form: result = mat * vec;
    int i;
    for (i = 0; i < rows; i++)
    {
        result[i] = vectors_dot_prod2(mat[i], vec, cols);
    }
}
 
#include <time.h>
#include <stdio.h>
 
int main(int argc, const char *argv[])
{
    static double mat[300][50];
    for (int i=0; i<300; i++)
        for (int j=0; j<50; j++)
            mat[i][j] = (i+j);
    static const double *matp[300];
    for (int i=0; i<300; i++)
        matp[i] = &mat[i][0];
 
    static double vector[50];
    for (int i=0; i<50; i++)
        vector[i] = i*i;
 
    static double result[300];
 
    clock_t start = clock();
    for (int n=0; n<100000; n++)
    {
        matrix_vector_mult(matp, vector, result, 300, 50);
    }
    clock_t stop = clock();
    printf("Computing time = %0.3fus\n",
           double(stop - start)/CLOCKS_PER_SEC/100000*1000000);
    return 0;
}