#include #include #include #include #include using namespace std; class timer { public: timer() : start_timing(hi_res_clock::now()) {} void reset() { start_timing = hi_res_clock::now(); } double elapsed() const { return(std::chrono::duration_cast (hi_res_clock::now() - start_timing).count()); } private: typedef std::chrono::high_resolution_clock hi_res_clock; typedef std::chrono::duration > second_; std::chrono::time_point start_timing; }; void print_time(const string & heading, const double & t) { cout << heading << " : "; cout.setf(ios::right); cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.width(10); cout.precision(6); cout << t << endl; } int main() { string heading; double t; timer timer_01; timer timer_02; heading = "\nProgram starts"; t = timer_01.elapsed(); print_time(heading, t); const int scale_factor = 15; const int max_row = scale_factor * 1024; const int max_col = scale_factor * 1024; array< array< double, max_col >, max_row > x; int r, c; int i, j; int nr = max_row; int nc = max_col; double tsum = 0.0; for (r = 0; r < nr; r++) for (c = 0; c < nc; c++) x[r][c] = 1.0; heading = "Initialisation"; t = timer_01.elapsed(); print_time(heading, t); for (i = 0; i < nr; i++) { for (j = 0; j < nc; j++) { tsum = tsum + x[i][j]; } } heading = "Summation "; t = timer_01.elapsed(); print_time(heading, t); cout << "Computed"; cout.width(25); cout.precision(0); cout << tsum << endl; cout << "Actual "; cout.width(24); cout << nr*nc << endl; return(0); }