C++ (Qt)template <class T, unsigned long Rows, unsigned long Columns>class matrix{public: typedef unsigned long size_type; matrix() { m_mtx = new T*[Rows]; for (size_type i = 0; i < Rows; ++i) m_mtx[i] = new T[Columns]; } ~matrix() { for (size_type i = 0; i < Rows; ++i) delete []m_mtx[i]; delete []m_mtx; } const T& operator()(size_type row, size_type column) const { return m_mtx[row][column]; } T& operator()(size_type row, size_type column) { return m_mtx[row][column]; } static size_type rows() { return Rows; } static size_type columns() { return Columns; }private: T **m_mtx;};
C++ (Qt)const unsigned long NUM_ROW = 1000000;const unsigned long NUM_COL = 20;
Bashmatrix<bool>: time (sec) = 0.09CBinMatrix: time (sec) = 0.22
Bashmatrix<char>: time (sec) = 0.1CBinMatrix: time (sec) = 0.22 matrix<char>: time (sec) = 0.11CBinMatrix: time (sec) = 0.22 matrix<char>: time (sec) = 0.11CBinMatrix: time (sec) = 0.24
Bashmatrix<int>: time (sec) = 0.23CBinMatrix: time (sec) = 0.22 matrix<int>: time (sec) = 0.25CBinMatrix: time (sec) = 0.23 matrix<int>: time (sec) = 0.24CBinMatrix: time (sec) = 0.23
C++ (Qt)if (arr[x][y]) ++sum;
C++ (Qt)sum += arr[x][y];
C++ (Qt)const unsigned long NUM_ROW = 20;const unsigned long NUM_COL = 1000000;
Bashmatrix<bool>: time (sec) = 3.5CBinMatrix: time (sec) = 4.89 matrix<bool>: time (sec) = 3.53CBinMatrix: time (sec) = 4.97 matrix<bool>: time (sec) = 3.55CBinMatrix: time (sec) = 4.91
Bashmatrix<bool>: time (sec) = 1.21CBinMatrix: time (sec) = 1.86 matrix<bool>: time (sec) = 1.2CBinMatrix: time (sec) = 1.85 matrix<bool>: time (sec) = 1.21CBinMatrix: time (sec) = 1.87