timer库包含3个组件:timer类,progress_timer类,progress_display(进度指示类).
timer:
#include <boost/timer.hpp>
1 #include <iostream> 2 #include "../boost_1_48_0/boost/timer.hpp" 3 using namespace std; 4 5 int main() 6 { 7 boost::timer t; 8 cout<<t.elapsed_max() / 3600<<"小时"<<endl; 9 cout<<t.elapsed_min()<<"秒"<<endl;10 cout<<"now print elapsed time:"<<t.elapsed()<<"秒"<<endl;11 12 return 0;13 }
源码:
1 class timer 2 { 3 public: 4 timer() { _start_time = std::clock(); } // postcondition: elapsed()==0 5 // timer( const timer& src ); // post: elapsed()==src.elapsed() 6 // ~timer(){} 7 // timer& operator=( const timer& src ); // post: elapsed()==src.elapsed() 8 void restart() { _start_time = std::clock(); } // post: elapsed()==0 9 double elapsed() const // return elapsed time in seconds10 { return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }11 12 double elapsed_max() const // return estimated maximum value for elapsed()13 // Portability warning: elapsed_max() may return too high a value on systems14 // where std::clock_t overflows or resets at surprising values.15 {16 return (double((std::numeric_limits<std::clock_t>::max)())17 - double(_start_time)) / double(CLOCKS_PER_SEC); 18 }19 20 double elapsed_min() const // return minimum value for elapsed()21 { return double(1)/double(CLOCKS_PER_SEC); }22 23 private:24 std::clock_t _start_time;25 }; // timer
timer不适合高精度计时,其精度依赖于OS和编译器,不能扩平台;也不合适于大跨度时间段的测量,可提供的最大时间跨度为几百个小时。若要以天,月,年为单位计时的话不要用timer,而用boost::date_time库。
progress_timer库:
继承自timer,会在析构时自动输出时间省去了timer手动调用elapsed()的操作,当然也可以调用elapsed()函数来输出时间。
若要在一个程序中测量多个时间,则可用{}来限定progress_timer的生命期。
1 #include <iostream> 2 #include "../boost_1_48_0/boost/progress.hpp" 3 using namespace std; 4 5 int main() 6 { 7 { 8 boost::progress_timer t; 9 int tmp = 0;10 for (int i=0; i<100000000; ++i)11 {12 tmp = i;13 }14 }15 16 {17 boost::progress_timer t;18 int tmp = 5;19 for (int i=0; i<100000000; ++i)20 {21 tmp *= i;22 tmp = 5;23 }24 }25 return 0;26 }
progress_timer的类定义如下:
1 class progress_timer : public timer, noncopyable2 {3 public:4 explicit progress_timer();5 progress_timer(std::ostream& os);6 ~progress_timer();7 };
第二个构造函数的参数为一个流,默认为std::cout,可以用其他流替换,就可以将时间输出到其他的流,比如ofstream, ostringstream。
progress_timer的输出精度只有小数点后2位,也就是百分之一秒。所以自己可以仿造一个progress_timer类。
1 #include <iostream> 2 #include "../boost_1_48_0/boost/progress.hpp" 3 #include "../boost_1_48_0/boost/static_assert.hpp" 4 using namespace std; 5 6 template <int N = 2> 7 class new_progress_timer : public boost::timer, boost::noncopyable 8 { 9 public:10 explicit new_progress_timer(std::ostream & os = std::cout): m_os(os)11 {12 BOOST_STATIC_ASSERT(N >= 0 && N <= 10);13 }14 15 ~new_progress_timer()16 {17 try18 {19 std::istream::fmtflags old_flags = 20 m_os.setf(std::istream::fixed, std::istream::floatfield);21 std::streamsize old_prec = m_os.precision(N);22 m_os<<elapsed()<<"s\n"<<std::endl;23 m_os.setf(old_flags);24 m_os.precision(old_prec);25 }26 catch (...)27 {}28 }29 30 private:31 // 流是不能复制的,所以用引用32 std::ostream& m_os;33 };34 35 template<>36 class new_progress_timer<2> : public boost::progress_timer37 {};38 39 40 int main()41 {42 {43 boost::progress_timer t;44 int tmp = 0;45 for (int i=0; i<100000000; ++i)46 {47 tmp = i;48 }49 }50 51 {52 boost::progress_timer t;53 int tmp = 5;54 for (int i=0; i<100000000; ++i)55 {56 tmp *= i;57 tmp = 5;58 }59 }60 61 {62 new_progress_timer<10> t(std::cout);63 int tmp = 5;64 for (int i=0; i<100000000; ++i)65 {66 tmp *= i;67 tmp = 5;68 }69 }70 71 return 0;72 }
progress_display显示程序进度类:
1 int main() 2 { 3 std::vector<std::string> v(1000000); 4 boost::progress_display pd(v.size()); 5 std::ofstream ofs; 6 ofs.open("test.txt"); 7 if (!ofs.is_open()) 8 { 9 return -1;10 }11 12 for (int i=0; i<v.size(); ++i)13 {14 ofs << i <<std::endl;15 ++pd;16 }17 18 ofs.close();19 20 return 0;21 }
date_time来太复杂。。。。掠过~~~:D
来源:https://www.cnblogs.com/kex1n/archive/2011/12/17/2291352.html