#include "boost/date_time/posix_time/posix_time.hpp" #include "timer.hpp" #include #include //Measure amount of processor time used by process class clockt_timer_traits { public: typedef std::clock_t time_rep; typedef boost::posix_time::time_duration time_duration_type; static time_rep init_zero() { return 0; } static time_rep get_current_time() { return std::clock(); } //turns out posix_time and CLOCKS_PER_SEC are normally equal //at 1000000 so fractional seconds is simply end - start //check with // std::cout << CLOCKS_PER_SEC << std::endl; // std::cout << time_duration::rep_type::res_adjust() << std::endl; // static time_duration_type make_time_duration(time_rep start, time_rep end) { assert(CLOCKS_PER_SEC == time_duration_type::rep_type::res_adjust()); return time_duration_type(0,0,0, end - start); } }; // Measures changes in wall time (not process time) class wallclock_timer_traits { public: typedef boost::posix_time::ptime time_rep; typedef boost::posix_time::time_duration time_duration_type; static time_rep init_zero() { return boost::posix_time::ptime(boost::gregorian::date(boost::date_time::min_date_time)); } static time_rep get_current_time() { return boost::posix_time::microsec_clock::local_time(); } static time_duration_type make_time_duration(time_rep start, time_rep end) { return (end - start); } }; template void run_timer() { timer_type t1; t1.start(); int j = 0; long n = 0; while (j < 20) { std::cout << t1.elapsed() << std::endl; for (int i=0; i<100000000; i++) { n = n + 1; } if (j == 10) { t1.stop(); } if (j == 12) { t1.restart(); } j++; } //trick the optimizer std::cout << n << std::endl; } int main() { using namespace boost::posix_time; std::cout << "Results for Clock_t timer" << std::endl; typedef timer clockt_timer; run_timer(); std::cout << "Results for High Precision wallclock timer" << std::endl; typedef timer wall_timer; run_timer(); return 0; }