Boost 计时器 定时器 速记

计时器 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;

本质上 定时器是依赖某种时钟的一系列接口 :

NameDescriton
async_waitStart an asynchronous wait on the timer.
basic_waitable_timerConstructor.Constructor to set a particular expiry time as an absolute time.Constructor to set a particular expiry time relative to now.
cancelCancel any asynchronous operations that are waiting on the timer.
cancel_oneCancels one asynchronous operation that is waiting on the timer.
expires_atGet the timer’s expiry time as an absolute time.Set the timer’s expiry time as an absolute time.
expires_from_nowGet the timer’s expiry time relative to now.Set the timer’s expiry time relative to now.
waitPerform a blocking wait on the timer.

时钟

  • boost::chrono::system_clock 系统时间
  • boost::chrono::process_real_cpu_clock CPU时间 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值