fork(1) download
  1. double vectors_dot_prod(const double *x, const double *y, int n)
  2. {
  3. double res = 0.0;
  4. int i;
  5. for (i = 0; i < n; i++)
  6. {
  7. res += x[i] * y[i];
  8. }
  9. return res;
  10. }
  11.  
  12. void matrix_vector_mult(const double **mat, const double *vec,
  13. double *result, int rows, int cols)
  14. { // in matrix form: result = mat * vec;
  15. int i;
  16. for (i = 0; i < rows; i++)
  17. {
  18. result[i] = vectors_dot_prod(mat[i], vec, cols);
  19. }
  20. }
  21.  
  22. double vectors_dot_prod2(const double *x, const double *y, int n)
  23. {
  24. double res = 0.0;
  25. int i = 0;
  26. for (; i <= n-4; i+=4)
  27. {
  28. res += (x[i] * y[i] +
  29. x[i+1] * y[i+1] +
  30. x[i+2] * y[i+2] +
  31. x[i+3] * y[i+3]);
  32. }
  33. for (; i < n; i++)
  34. {
  35. res += x[i] * y[i];
  36. }
  37. return res;
  38. }
  39.  
  40. void matrix_vector_mult2(const double **mat, const double *vec,
  41. double *result, int rows, int cols)
  42. { // in matrix form: result = mat * vec;
  43. int i;
  44. for (i = 0; i < rows; i++)
  45. {
  46. result[i] = vectors_dot_prod2(mat[i], vec, cols);
  47. }
  48. }
  49.  
  50. #include <time.h>
  51. #include <stdio.h>
  52.  
  53. int main(int argc, const char *argv[])
  54. {
  55. static double mat[300][50];
  56. for (int i=0; i<300; i++)
  57. for (int j=0; j<50; j++)
  58. mat[i][j] = (i+j);
  59. static const double *matp[300];
  60. for (int i=0; i<300; i++)
  61. matp[i] = &mat[i][0];
  62.  
  63. static double vector[50];
  64. for (int i=0; i<50; i++)
  65. vector[i] = i*i;
  66.  
  67. static double result[300];
  68.  
  69. clock_t start = clock();
  70. for (int n=0; n<100000; n++)
  71. {
  72. matrix_vector_mult(matp, vector, result, 300, 50);
  73. }
  74. clock_t stop = clock();
  75. printf("Computing time = %0.3fus\n",
  76. double(stop - start)/CLOCKS_PER_SEC/100000*1000000);
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 2.95s 2844KB
stdin
Standard input is empty
stdout
Computing time = 29.700us