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;
- class SquareMatrix {
- public:
- class Row;
- friend class Row;
- SquareMatrix(unsigned int side)
- : side_(side), values_(new int[side*side])
- {
- }
- ~SquareMatrix() { delete [] values_; }
- SquareMatrix(const SquareMatrix &) = delete;
- SquareMatrix(SquareMatrix &&other)
- : side_(other.side_), values_(other.values_)
- {
- other.values_ = nullptr;
- }
- const SquareMatrix &operator =(const SquareMatrix &) = delete;
- const SquareMatrix &operator =(SquareMatrix &&) = delete;
- unsigned int size() const { return side_ ; }
- inline Row operator [](unsigned int row);
- inline const Row operator [](unsigned int row) const;
- private:
- const unsigned int side_;
- int *values_;
- };
- class SquareMatrix::Row {
- friend class SquareMatrix;
- public:
- const Row &operator =(const Row &) = delete;
- const Row &operator =(Row &&) = delete;
- int &operator [](unsigned int col) {
- return matrix_.values_[row_ * matrix_.side_ + col];
- }
- int operator [](unsigned int col) const {
- return matrix_.values_[row_ * matrix_.side_ + col];
- }
- private:
- SquareMatrix &matrix_;
- const unsigned int row_;
- Row(Row &&) = default;
- Row(SquareMatrix &m, unsigned int row)
- : matrix_(m), row_(row)
- {
- }
- };
- inline SquareMatrix::Row SquareMatrix::operator [](unsigned int row)
- {
- return Row(*this, row);
- }
- inline const SquareMatrix::Row SquareMatrix::operator [](unsigned int row) const
- {
- return Row(*const_cast<SquareMatrix *>(this), row);
- }
- 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];
- {
- SquareMatrix A(m), B(m), product(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";
- }
- }
- 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;
- }
Add Comment
Please, Sign In to add comment