#ifndef TIMER_H #define TIMER_H 1 // (c) 1999 Jeffrey D. Oldham. ALL rights reserved. // Oldham, Jeffrey D. // 1999 Dec 11 // CS 1321 // // Code to Print the Total Running of a Process // To use this code, // 1. Copy this file to the directory containing the rest of your code. // 2. Add #include "timer.h" near the top of your code. // 3. Add a call to the function timer(cout) at the end of your code. // You can use any other valid ostream (or ofstream). #include #include #include // timer() prints the total running time of the program // input <- output stream // output-> none void timer(ostream & out) { struct rusage rusage; // resource use structure if (getrusage(RUSAGE_SELF, &rusage)) { cerr << "getrusage() failed.\n"; } else { const long umsec = rusage.ru_utime.tv_sec * 1000 + rusage.ru_utime.tv_usec / 1000; const long smsec = rusage.ru_stime.tv_sec * 1000 + rusage.ru_stime.tv_usec / 1000; out << "c total time:\t" << umsec + smsec << " msec\n"; } return; } #endif // TIMER_H