// 64 bit integer index #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; } void compute(long long int nr) { string heading; double t; double tsum=0; timer timer_01; timer timer_02; timer_01.reset(); long long int r; vector x(nr,1.0); heading = "Allocation "; t = timer_01.elapsed(); print_time(heading, t); timer_01.reset(); for (r = 0; r < nr; r++) { tsum = tsum + x[r]; } heading = "Summation "; t = timer_01.elapsed(); print_time(heading, t); cout << "Computed sum "; cout.width(25); cout.precision(0); cout << tsum << endl; } int main() { string heading; double t; timer timer_01; long long int one_gb = 1024 * 1024 * 1024; long long int nr = 1 * one_gb; int i; int j=4; heading = "\nProgram starts"; t = timer_01.elapsed(); print_time(heading, t); cout.imbue(std::locale("")); for (i=1;i<=j;i++) { cout << endl; compute(nr); nr = nr * 2; } heading = "\nProgram ends"; t = timer_01.elapsed(); print_time(heading, t); return(0); }