Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib> // for random number generator
- #include <ctime>
- #include <fstream> // for writing to the file
- using namespace std;
- int main()
- {
- int array_sizes[6] = {32,64,128,256,512,1024};
- int m;
- ofstream results; //output the results
- results.open ("results.txt");
- for(int i = 0; i < 6; i++){
- for (int k = 0; k < 3; k ++){
- clock_t start = clock();
- m = array_sizes[i];
- int **A = new int*[m];
- int **B = new int*[m];
- int **product = new int*[m];
- for ( int i = 0 ; i < m ; i++)
- {
- A[i] = new int[m];
- B[i] = new int[m];
- product[i] = new int[m];
- }
- // Initialize A[I][K]
- // I x K == row * inner
- for(int row = 0; row < m; row++) {
- for(int inner = 0; inner < m; inner++){
- A[row][inner] = 5;
- }
- }
- for(int row = 0; row < m; row++) {
- for(int inner = 0; inner < m; inner++){
- B[row][inner] = 3;
- //cout << B[row][inner] << " ";
- }
- //cout << "\n";
- }
- // main routine for multiplication
- for(int row = 0; row < m; row++) {
- for(int col = 0; col < m; col++) {
- product[row][col] = 0;
- for(int inner = 0; inner < m; inner++) {
- product[row][col] += A[row][inner] * B[inner][col];
- }
- //cout << product[row][col] << " ";
- }
- //cout << "\n";
- }
- //Deallocate
- for ( int i = 0 ; i < m ; i++)
- {
- delete[] A[i];
- delete[] B[i];
- delete[] product[i];
- }
- delete[] A;
- delete[] B;
- delete[] product;
- clock_t end = clock();
- double cpu_time = static_cast<double>(end - start)/CLOCKS_PER_SEC;
- results << cpu_time << endl;
- }
- results << m << endl;
- }
- results.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement