Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. //Get args
  9. unsigned int matrixSize = stoi(argv[1]);
  10. unsigned int repeatAmount = stoi(argv[1]);
  11.  
  12. cout << "matrixSize: " << matrixSize << endl;
  13. cout << "repeatAmount: " << repeatAmount << endl;
  14.  
  15. //init
  16. double *multiplicationVektor = new double[matrixSize];
  17. double *multiplicationResult = new double[matrixSize];
  18. double **multiplicationMatrix = new double *[matrixSize];
  19.  
  20. for (int i = 0; i < matrixSize; i++)
  21. {
  22. multiplicationMatrix[i] = new double[matrixSize];
  23. }
  24.  
  25. //fill data
  26. for (int row = 0; row < matrixSize; row++)
  27. {
  28. for (int column = 0; column < matrixSize; column++)
  29. {
  30. multiplicationMatrix[row][column] = 300 + row + row * column;
  31. }
  32. }
  33. for (int row = 0; row < matrixSize; row++)
  34. {
  35. multiplicationVektor[row] = 50 + 3 * row;
  36. }
  37.  
  38. //####alg 1
  39. //track time
  40. clock_t t1, t2;
  41. t1 = clock();
  42.  
  43. //math
  44. for (int k = 0; k < repeatAmount; k++)
  45. {
  46.  
  47. for (int row = 0; row < matrixSize; row++)
  48. {
  49. multiplicationResult[row] = 0;
  50. for (int column = 0; column < matrixSize; column++)
  51. {
  52. multiplicationResult[row] += multiplicationMatrix[row][column] * multiplicationVektor[column];
  53. }
  54. }
  55. }
  56.  
  57. //measure time
  58. t2 = clock();
  59. cout << "alg 1 laikas: " << (float)(t2 - t1) / (float)CLOCKS_PER_SEC << endl;
  60.  
  61. //###alg 2
  62. t1 = clock();
  63.  
  64. //math
  65. for (int k = 0; k < repeatAmount; k++)
  66. {
  67.  
  68. for (int row = 0; row < matrixSize; row++)
  69. multiplicationResult[row] = 0;
  70.  
  71. for (int column = 0; column < matrixSize; column++)
  72. {
  73. for (int row = 0; row < matrixSize; row++)
  74. {
  75. multiplicationResult[row] += multiplicationMatrix[row][column] * multiplicationVektor[column];
  76. }
  77. }
  78. }
  79.  
  80. //measure time
  81. t2 = clock();
  82. cout << "alg 2 laikas: " << (float)(t2 - t1) / (float)CLOCKS_PER_SEC << endl;
  83.  
  84. //out
  85. // for (int i = 0; i < N; i++)
  86. //
  87. // cout << C[i] << endl;
  88. // }
  89.  
  90. //cleanup
  91. for (int row = 0; row < matrixSize; row++)
  92. {
  93. delete[] multiplicationMatrix[row];
  94. }
  95. delete[] multiplicationMatrix;
  96. delete[] multiplicationVektor;
  97. delete[] multiplicationResult;
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement