boost timer
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的更多相关文章
- boost::timer库使用
boost::timer boost库定时器使用,需要在编译时加相关链接库 -lboost_timer -lboost_system boost::timer::cpu_timer 和boost::t ...
- boost::timer demo
#include <iostream> #include <boost/timer.hpp> //timer的头文件 using namespace boost; //打开bo ...
- boost timer 定时器 Operation cancel !
前面段时间处理一个定时器时,老是提示 操作取消. 硬是没搞明白为什么! 其实我遇到的这个情况很简单就是(boost::asio::deadline_timer timer)这个变量的生命同期结束了,对 ...
- boost timer代码学习笔记
socket连接中需要判断超时 所以这几天看了看boost中计时器的文档和示例 一共有五个例子 从简单的同步等待到异步调用超时处理 先看第一个例子 // timer1.cpp: 定义控制台应用程序的入 ...
- 初探boost之timer库学习笔记
timer 使用方法 #include <boost/timer.hpp> #include <iostream> using namespace std; usi ...
- boost 学习笔记 2: timer
boost 学习笔记 2: timer copy from:http://einverne.github.io/post/2015/12/boost-learning-note-2.html 1:ti ...
- boost之时间timer
C++一直缺乏对时间和日期的处理能力,一般借助于C的struct tm和time():timer包含三个类其中timer,progress_timer是计时器类,进度指示类是progress_disp ...
- Boost中的Timer的使用——计算时间流逝
使用Boost中的Timer库计算程序的运行时间 程序开发人员都会面临一个共同的问题,即写出高质量的代码完毕特定的功能.评价代码质量的一个重要标准就是算法的运行效率,也就是算法的运行时间.为了可靠的提 ...
- Ubuntu 14.04 编译安装 boost 1.58
简介 Boost is a set of libraries for the C++ programming language that provide support for tasks and s ...
随机推荐
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- stl vector创建二维数组
vector<vector<); for (auto it = v.begin(); it != v.end(); it++) { ; (*it).reserve();//预留空间为5,但 ...
- sts测试流程
测试目的: 测试安全补丁打上了没 测试前提: 1.发货版本,user debug版本,相应安全补丁已合入,测试工具与安全补丁是对应的 2.selinux:Enable 3.连接ADB,stay awa ...
- MySQL DDL-对库和表的操作
#DDL /* 数据定义语言 库和表的管理 一.库的管理 创建.修改.删除 二.表的管理 创建.修改.删除 创建:create 修改: alter 删除:drop */ 一.库的管理 #1.库的创建 ...
- 编程语言-Python-GUI
PyQt5 import sys from PyQt5 import QtWidgets,QtCore app = QtWidgets.QApplication(sys.argv) widget = ...
- mysql 5.1.34
在make之前,将MAKEFILE中的do abi check注释,不要注释名字... mysql 5.1 编译安装 分类: mysql2012-04-06 13:01 17175人阅读 评论(0) ...
- 异步请求jquery action
package com.tarena.action; import java.util.HashMap;import java.util.Map; import javax.annotation.Re ...
- 《剑指offer》面试题10 二进制中1的个数 Java版
书中方法一:对于每一位,用1求与,如果为1表明该位为1.一共要进行32次,int4字节32位. public int check(int a){ int result = 0; int judge = ...
- 12、numpy——数学函数
NumPy 数学函数 NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等. 1.三角函数 NumPy 提供了标准的三角函数:sin().cos().tan(). i ...
- hdu4352 XHXJ's LIS(数位dp)
题目传送门 XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...