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 ...
随机推荐
- PHP-操作json
输出 json 文件中文处理 <?php $json_array = array(); // 1.转换为json字符串(不自动转换为unicode编码) if (version_compare( ...
- 《图解设计模式》读书笔记1-1 Iterator模式
目录 迭代器模式的类图 类图的解释 迭代器模式的代码 解释 原因 思想 迭代器模式的类图 类图的解释 名称 说明 Aggregate 集合接口,有提供迭代器的方法 Iterator 迭代器接口,提供迭 ...
- JSP表单提交 与 接受显示
Demo01.jsp 提交表单输入的信息至 Demo02.jsp方法一 1 <%@ page language="java" contentType="text/h ...
- ECG 项目预研
1. 数据的采集 智能安全帽,流数据,鉴于数据量大,应该是采集到云平台上,然后在云平台上对数据处理,是一种典型的物联网+大数据应用场景,考虑使用AWS或者阿里云,然后搭建Hadoop/Spark 环境 ...
- 应用安全-XXE(XML外部实体注入)攻防整理
libxml2..1及以后,默认不解析外部实体.测试的时候window下使用php5.(libxml Version ), php5.(libxml Version ).Linux中需要将libxml ...
- C# 身份证号码验证正则和验证函数
做身份证验证的时候要求能够按照标准18位身份证验证,普通正则表达式不能满足需求,所以在网上找到了这个函数,很好用,虽然还是有漏洞,不过一般乱填的号码都能被屏蔽掉 身份证验证函数(标准18位验证) pr ...
- Python入门习题5.蒙特卡罗方法计算圆周率
#CalPi.py from random import random from math import sqrt from time import clock DARTS = 10000000 hi ...
- Day3---Python的time库的一些简单函数以及用法
time库的一些函数 time.time () : 获取当前时间戳,即计算机内部时间值,浮点数 >>>import time >>> time.time() 1 ...
- python 列表总结大全
1定义 names=[] names=[1,2,1,1,1,] names=[1.'10'.[1,1]] 2添加元素 names.append() names.insert(0,10) names.e ...
- 修改xampp中的MySQL密码
1.开启MySQL服务后,点击XAMPP Control Panel上的Admin按钮 2.依次点击"账户"--最后一个"修改权限"--修改密码 3.输入两次相 ...