#ifndef StopWatch__ #define StopWatch__ /*** Example use of StopWatch main() { int i, a=0; StopWatch w; w.start(); ... first measured part w.lap("first part"); ... second measured part w.lap("second part"); ... third measured part w.lap("third part"); ... fourth measured part w.stop(); // could be w.stop("fourth part"); } ***/ #include extern "C" { #include } using namespace std; class StopWatch { public: StopWatch(int laps = 20) { next = 0; maxLaps = laps; name = new const char* [laps] ; times = new long long [laps]; } void start(const char* msg = starting) // start the timer { lap(msg); } void lap(const char* msg = 0, int printflag = 0) // record an intermediate time { if (next < maxLaps ) { times[next] = gethrvtime(); name[next] = msg; next++; if (printflag) display(printflag); } else { display(); next = 0; lap(msg, printflag); } } void stop(const char* msg = stopping, int printflag = 2) // record a final time { lap(msg,printflag); } // this needs to be fixed up fancier void display(int printflag = 2, std::ostream& os = cout, double unit = .000000001) { os << endl; os << " Elapsed user time" << endl; os << endl; os.setf( ios::fixed, ios::floatfield ); for (int j = (printflag == 1 ? next-1 : 1); j < next; j++) { os << " " << (times[j]-times[j-1])*unit << " secs - " << name[j] << endl; } if (next > 0 && printflag > 1) { os << " " << "--------------------------" << endl; os << " " << (times[next-1]-times[0])*unit << " secs - " << "Total time" << endl; os << endl; } os.unsetf( ios::fixed ); } protected: long long * times; const char* * name; int next; int maxLaps; static const char* starting;// = "StopWatchStart"; static const char* stopping;// = "StopWatchStop"; }; const char* StopWatch::starting = "StopWatchStart"; const char* StopWatch::stopping = "StopWatchStop"; #endif