Boost cpu_timer 学习笔记

cpu_timer类和auto_cpu_timer类用于精确计时,有elapsedstartis_stopped等方法。在elapsed方法中,返回的不再是一个数字,而是一个struct cpu_times,这个结构体中,定义为:

struct cpu_times
{
    nanosecond_type wall;
    nanosecond_type user;
    nanosecond_type system;

    void clear() {wall = user = system = 0LL; }
};

boost官方文档的说法,wall指的是程序运行的真实时间,user指的是用户CPU时间,system指系统CPU时间。其中,真实时间易受其它程序运行干扰,是不稳定的。如果是衡量算法运行时间,更好的度量是usersystem之和。从变量类型可以看出,所以的时间单位均为纳秒(ns)。

默认的输出格式为:

5.713010s wall, 5.709637s user + 0.000000s system = 5.709637s CPU (99.9%)

文档解释为:

In other words, this program ran in 5.713010 seconds as would be measured by a clock on the wall, the operating system charged it for 5.709637 seconds of user CPU time and 0 seconds of system CPU time, the total of these two was 5.709637, and that represented 99.9 percent of the wall clock time.

格式可以自定义,默认的格式定义为:

" %ws wall, %us user + %ss system = %ts CPU (%p%)\n"

含义为:

SequenceReplacement value
%wtimes.wall
%utimes.user
%stimes.system
%ttimes.user + times.system
%pThe percentage of times.wall represented by times.user + times.system

auto_cpu_timer定义的时候,可将格式内容传入构造函数,以控制输出格式。

而对于cpu_timer类,有format函数,定义为:

std::string format(short places, const std::string& format);
std::string format(short places = default_places);

其中,places控制时间的小数点位数,format就是刚刚的格式字符串了。

主要内容就是这些了,现在看一下一个小例子吧:

#include <iostream>
#include <boost/timer/timer.hpp>
#include <cmath>

using namespace std;
using namespace boost;

int main()
{
    timer::cpu_timer t;
    timer::auto_cpu_timer auto_timer(6,
            "%ws real time\n");
    int a;

    for (long i = 0; i < 100000000; ++i)
        a = sqrt(i * i); // spend some time

    cout << "is started: " << (t.is_stopped() ?
        "no" : "yes") << endl;
    cout << t.format(2, "%us user + %ss system "
            "= %ts(%p%)") << endl;

    return 0;
}

需要注意的是,编译时需要链接boost相关库,而不是以前的可以直接运行:

g++ timer.cpp -o timer -lboost_timer -lboost_system

输出为:

is started: yes
2.08s user + 0.00s system = 2.08s(99.6%)
2.088596s real time
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
boost::asio::high_resolution_timer是一个定时器类,用于在boost::asio库中进行高分辨率的定时操作。 boost::asio库是一个用于网络和异步编程的C++库。它提供了丰富的功能,包括异步IO操作、定时器、socket通信等。其中,boost::asio::high_resolution_timer是其中的一个定时器类,它使用了高分辨率的时钟来进行精确的定时操作。 使用boost::asio::high_resolution_timer,我们可以创建一个定时器对象,并设置定时的时间间隔。可以使用成员函数expires_from_now()指定定时的时间间隔,参数为一个duration类型的对象,表示时间间隔的长度。 例如,以下代码创建了一个定时器对象timer,设置了定时时间间隔为1秒: boost::asio::high_resolution_timer timer(io_context); timer.expires_from_now(boost::posix_time::seconds(1)); 然后,我们可以调用定时器对象的async_wait()函数来启动定时器,并指定一个回调函数,在定时器超时时被调用。回调函数可以是一个lambda函数,也可以是一个函数对象。 例如,以下代码定义了一个lambda函数作为回调函数: timer.async_wait([](const boost::system::error_code& ec) { if (!ec) { // 定时器超时,执行相应操作 } }); 在定时器超时时,回调函数会被触发,并执行相应操作。 总之,boost::asio::high_resolution_timer是一个用于高分辨率定时操作的定时器类,可以帮助我们在异步编程中进行精确的定时操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值