Boost.Timer provides clocks to measure code performance. At first, it may seem like this library competes with Boost.Chrono. However, while Boost.Chrono provides clocks to measure arbitrary periods, Boost.Timer measures the time it takes to execute code. Although Boost.Timer uses Boost.Chrono, when you want to measure code performance, you should use Boost.Timer rather than Boost.Chrono.

1. cpu_timer

#include <boost/timer/timer.hpp>
#include <iostream>
#include <cmath> using namespace boost::timer; int main()
{
cpu_timer timer; for (int i = ; i < ; ++i)
std::pow(1.234, i);
std::cout << timer.format() << std::endl;
return ;
}

Measurement starts when boost::timer::cpu_timer is instantiated. You can call the member function format() at any point to get the elapsed time.

输出为:

0.057313s wall, 0.050000 user + 0.000000s system = 0.050000 CPU (87.2%)

The wall time is the time which passes according to a wall clock. The CPU time says how much time the program spent executing code. CPU time is divided between time spent in user space and time spent in kernel space.

2. stopping and resuming timers

#include <boost/timer/timer.hpp>
#include <iostream>
#include <cmath> using namespace boost::timer; int main()
{
cpu_timer timer; for (int i = ; i < ; ++i)
std::pow(1.234, i);
std::cout << timer.format() << std::endl; timer.stop(); for (int i = ; i < ; ++i)
std::pow(1.234, i);
std::cout << timer.format() << std::endl; timer.resume(); for (int i = ; i < ; ++i)
std::pow(1.234, i);
std::cout << timer.format() << std::endl;
return ;
}

boost::timer::cpu_timer provides the member functions stop() and resume(), which stop and resume timers. boost::timer::cpu_timer also provides a member function start(). If you call start(), the timer restarts from zero.

3. getting wall and cpu time as a tuple

#include <boost/timer/timer.hpp>
#include <iostream>
#include <cmath> using namespace boost::timer; int main()
{
cpu_timer timer; for (int i = ; i < ; ++i)
std::pow(1.234, i); cpu_times times = timer.elapsed();
std::cout << times.wall << std::endl;
std::cout << times.user << std::endl;
std::cout << times.system << std::endl;
return ;
}

boost::timer::cpu_timer provides the member function elapsed(). elapsed() returns a tuple of type boost::timer::times. This tuple has three member varibles: wall, user and system. These member variables contain the wall and CPU times in nanoseconds. boost::timer::times provides the member function clear() to set wall, user and system to 0.

4. auto_cpu_timer

#include <boost/timer/timer.hpp>
#include <cmath> using namespace boost::timer; int main()
{
auto_cpu_timer timer; for (int i = ; i < ; ++i)
std::pow(1.234, i);
return ;
}

The destructor of this class stops measuring time and writes the time to the standard output stream.

boost timer的更多相关文章

  1. boost::timer库使用

    boost::timer boost库定时器使用,需要在编译时加相关链接库 -lboost_timer -lboost_system boost::timer::cpu_timer 和boost::t ...

  2. boost::timer demo

    #include <iostream> #include <boost/timer.hpp> //timer的头文件 using namespace boost; //打开bo ...

  3. boost timer 定时器 Operation cancel !

    前面段时间处理一个定时器时,老是提示 操作取消. 硬是没搞明白为什么! 其实我遇到的这个情况很简单就是(boost::asio::deadline_timer timer)这个变量的生命同期结束了,对 ...

  4. boost timer代码学习笔记

    socket连接中需要判断超时 所以这几天看了看boost中计时器的文档和示例 一共有五个例子 从简单的同步等待到异步调用超时处理 先看第一个例子 // timer1.cpp: 定义控制台应用程序的入 ...

  5. 初探boost之timer库学习笔记

    timer   使用方法     #include <boost/timer.hpp> #include <iostream> using namespace std; usi ...

  6. boost 学习笔记 2: timer

    boost 学习笔记 2: timer copy from:http://einverne.github.io/post/2015/12/boost-learning-note-2.html 1:ti ...

  7. boost之时间timer

    C++一直缺乏对时间和日期的处理能力,一般借助于C的struct tm和time():timer包含三个类其中timer,progress_timer是计时器类,进度指示类是progress_disp ...

  8. Boost中的Timer的使用——计算时间流逝

    使用Boost中的Timer库计算程序的运行时间 程序开发人员都会面临一个共同的问题,即写出高质量的代码完毕特定的功能.评价代码质量的一个重要标准就是算法的运行效率,也就是算法的运行时间.为了可靠的提 ...

  9. Ubuntu 14.04 编译安装 boost 1.58

    简介 Boost is a set of libraries for the C++ programming language that provide support for tasks and s ...

随机推荐

  1. 台哥原创:java 扫雷源码

    扫雷,十年前大学时候开发的,界面参照的电脑自带扫雷游戏. 一直是我最喜欢的单机游戏,现在微软的新系统都不能玩了. 幸好还有自己开发的,可以过下瘾.程序员就有这点好处嘛. ​ 这几年陆陆续续,把这个扫雷 ...

  2. leetcode 141. 环形链表(C++)

    给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 1: 输入: ...

  3. 16/8/21_PHP-有关类函数,this,static,面向对象思想介绍

    class_exists():判断某个类是否存在(定义过) interface_existe():判断接口是否存在 get_class():获取某个对象的"所属类名" get_pa ...

  4. iframe父窗口和子窗口的调用方法

    iframe 父窗口和子窗口的调用方法父窗口调用子窗口 iframe_name.iframe_document_object.object_attribute = attribute_value 例子 ...

  5. 如何根据字典值的大小,对字典中的项排序---Python数据结构与算法相关问题与解决技巧

    实际案例: 某班英语成绩以字典形式存储为: { 'LiLei' : 90, 'Jim' : 88, 'Lucy': 92 } 如何根据成绩高低,计算学生排名 -- 根据分数,进行排名,并且把排名信息添 ...

  6. 继承Process类,计算累加和以及阶乘

    #定义一个类 继承Process类 from multiprocessing import Process import os class Download(Process): def __init_ ...

  7. [Python3 填坑] 013 几个类相关函数的举例

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 issubclass() 2.2 isinstance() 2.3 hasattr() 2.4 getattr() 2.5 setatt ...

  8. yum安装时出现No more mirrors to try.

    可能原因:可能是不正常删除造成的 解决方法: yum clean allyum makecacheyum -y update 然后重新安装

  9. 【Linux】php7.2.8 + xdebug + composer + php代码覆盖率 + jenkins配置 (实操记录,亲测可用)

        [一.linux安装php 7.2.8] 1.wget http://nginx.org/download/nginx-1.9.9.tar.gz              # nginx可不安 ...

  10. Spark链接hive时 “HikariCP” 问题

    IDE本地调试和spark-shell调试报错: Caused by: org.datanucleus.exceptions.NucleusUserException: The connection ...