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 ...
随机推荐
- activity_main.xml 添加自己画的view 组件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- vue鼠标修饰符
鼠标左键事件 <div @click.left="mouseClick" style="border: solid 1px red; width:500px; he ...
- linux-批量修改目录下后缀shell
#!/bin/bashcd /optrename .sh .shell *.shecho "后缀修改成功"
- vs报错 "多步操作产生错误。请检查每一步的状态值"
今天在开发一个插件图表控件,在实例化后向数据库Update时候,报出此错误,刚开始以为是我用的异步方法,在调用程序的句柄的时候的线程问题,索性改成了同步方法,仍然报出此错误.后来Debug和排错,定位 ...
- FCKEditor添加字体
默认情况下,FCKEditor在进行文本编辑时,无法使用中文字体.自个摸索了下:打开 fckconfig.js 文件 找到第154行(应该是),会发现:FCKConfig.FontNames = 'A ...
- C++ STL:优先队列的使用详解
堆是一个很重要的数据结构,那么我们如何更加简洁的去写大根/小根堆呢? 对于很多语言来说,只能一步一步手打,但是对于C++来说,写大根小根堆就简便得多,因为C++中有一个容器叫做priority_que ...
- 爬虫之requests 高级用法
1. 文件上传 import requests files = {'file': open('favicon.ico', 'rb')} r = requests.post("http://h ...
- servlet--三大域
requset \ session servletContext application
- Linux中ssh及scp的连接
1. 当你想获取另外一台电脑上的数据时,可以使用这个命令 scp -P 10022 root@172.30.83.173:~/ubuntu1.tar ./ -r 代表传输文件夹,直接传文件可以不加 ...
- Join的7中情况
一.左外连接 SELECT * FROM A LEFT JOIN B ON A.KEY = B.KEY 二.右外连接 SELECT * FROM A RIGHT JOIN B ON A.KEY = B ...