Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- using namespace std;
- int main(int argc, char *argv[])
- {
- //Get args
- unsigned int matrixSize = stoi(argv[1]);
- unsigned int repeatAmount = stoi(argv[1]);
- cout << "matrixSize: " << matrixSize << endl;
- cout << "repeatAmount: " << repeatAmount << endl;
- //init
- double *multiplicationVektor = new double[matrixSize];
- double *multiplicationResult = new double[matrixSize];
- double **multiplicationMatrix = new double *[matrixSize];
- for (int i = 0; i < matrixSize; i++)
- {
- multiplicationMatrix[i] = new double[matrixSize];
- }
- //fill data
- for (int row = 0; row < matrixSize; row++)
- {
- for (int column = 0; column < matrixSize; column++)
- {
- multiplicationMatrix[row][column] = 300 + row + row * column;
- }
- }
- for (int row = 0; row < matrixSize; row++)
- {
- multiplicationVektor[row] = 50 + 3 * row;
- }
- //####alg 1
- //track time
- clock_t t1, t2;
- t1 = clock();
- //math
- for (int k = 0; k < repeatAmount; k++)
- {
- for (int row = 0; row < matrixSize; row++)
- {
- multiplicationResult[row] = 0;
- for (int column = 0; column < matrixSize; column++)
- {
- multiplicationResult[row] += multiplicationMatrix[row][column] * multiplicationVektor[column];
- }
- }
- }
- //measure time
- t2 = clock();
- cout << "alg 1 laikas: " << (float)(t2 - t1) / (float)CLOCKS_PER_SEC << endl;
- //###alg 2
- t1 = clock();
- //math
- for (int k = 0; k < repeatAmount; k++)
- {
- for (int row = 0; row < matrixSize; row++)
- multiplicationResult[row] = 0;
- for (int column = 0; column < matrixSize; column++)
- {
- for (int row = 0; row < matrixSize; row++)
- {
- multiplicationResult[row] += multiplicationMatrix[row][column] * multiplicationVektor[column];
- }
- }
- }
- //measure time
- t2 = clock();
- cout << "alg 2 laikas: " << (float)(t2 - t1) / (float)CLOCKS_PER_SEC << endl;
- //out
- // for (int i = 0; i < N; i++)
- //
- // cout << C[i] << endl;
- // }
- //cleanup
- for (int row = 0; row < matrixSize; row++)
- {
- delete[] multiplicationMatrix[row];
- }
- delete[] multiplicationMatrix;
- delete[] multiplicationVektor;
- delete[] multiplicationResult;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement