计时器 boost.Timer
接口
elapsed()
报告当前跑了多久了。 结果包括:
struct cpu_times
{
nanosecond_type wall;
nanosecond_type user;
nanosecond_type system;
void clear() { wall = user = system = 0LL; }
};
start()
开始计时stop()
结束计时resume()
重新开始计时
例子
#include <boost/timer/timer.hpp>
#include <chrono>
#include <thread>
int main() {
boost::timer::cpu_timer the_timer;
do {
std::cout<<the_timer.elapsed().wall<<std::endl;
the_timer.stop();
the_timer.resume();
std::this_thread::sleep_for(std::chrono::seconds(1));
}while(1);
return 0 ;
}
包装的辅助类 auto_cpu_timer – 析构函数汇报计时
#include <boost/timer/timer.hpp>
#include <cmath>
int main()
{
boost::timer::auto_cpu_timer t;
for (long i = 0; i < 100000000; ++i)
std::sqrt(123.456L); // burn some time
return 0;
}
定时器 boost.asio.xxx_timer
有多种定时器:
- deadline_timer
typedef basic_deadline_timer< boost::posix_time::ptime > deadline_timer;
- high_resolution_timer
typedef basic_deadline_timer< chrono::high_resolution_clock > deadline_timer;
- steady_timer
typedef basic_deadline_timer< chrono::steady_clock > deadline_timer;
- system_timer
typedef basic_deadline_timer< chrono::steady_clock > deadline_timer;
本质上 定时器是依赖某种时钟的一系列接口 :
Name | Descriton |
---|---|
async_wait | Start an asynchronous wait on the timer. |
basic_waitable_timer | Constructor.Constructor to set a particular expiry time as an absolute time.Constructor to set a particular expiry time relative to now. |
cancel | Cancel any asynchronous operations that are waiting on the timer. |
cancel_one | Cancels one asynchronous operation that is waiting on the timer. |
expires_at | Get the timer’s expiry time as an absolute time.Set the timer’s expiry time as an absolute time. |
expires_from_now | Get the timer’s expiry time relative to now.Set the timer’s expiry time relative to now. |
wait | Perform a blocking wait on the timer. |
时钟
- boost::chrono::system_clock 系统时间
- boost::chrono::process_real_cpu_clock CPU时间 。