环境搭建---查看我另一篇
本人测试环境Ubuntu18.04
#include <iostream>
#include <boost/version.hpp>
#include <boost/config.hpp>
using namespace std;
int main(void)
{
cout << BOOST_VERSION << endl;
cout << BOOST_LIB_VERSION << endl;
cout << BOOST_PLATFORM << endl;
cout << BOOST_COMPILER << endl;
cout << BOOST_STDLIB << endl;
return 0;
}
~/Boost/test$a.out
106700
1_67
linux
GNU C++ version 5.4.0 20160609
GNU libstdc++ version 20160609
noncopyable -- 不能复制的类(单例)
一、时间类
1.timer库
头文件: #include <boost/timer.hpp>
名字空间:using namespace boost;
timer类实现:
class timer{
public:
// 构造函数
timer()
{
_start_time = std:clock();
}
// 重新开始计时
void restart()
{
_start_time = std:clock();
}
// 已流逝的时间
// CLOCKS_PER_SEC -- macOS、linux下为1,000,000(精确微秒)
-- Windows下为1,000(精确毫秒)
double elapsed() const
{
return double(std:clock() - _start_time) / CLOCKS_PER_SEC;
}
// 测量的最小时间单位
double elapsed_min() const
{
return double(1) / double(CLOCKS_PER_SEC);
}
// 可测量的最大时间
// numeric_limits -- 标准库中数值极限类
double elapsed_max() const
{
return (double((std:numeric_limits<std:: clock_t>::max)()) - double(_start_time) / double(CLOCKS_PER_SEC);
}
private:
std:clock_t _start_time; // 声明成员变量_start_time;
};
timer类没有析构函数
使用建议:
timer接口简单,轻巧好用,适用于大部分要求不高的程序计时任务。
timer不适合高精度的时间测量任务,它的精度依赖于操作系统或编译器,难以跨平台。
timer也不适合大跨度时间段测量,如(天,月,年),推荐使用cpu_timer组件
#include <boost/timer.hpp>
#include <iostream>
using namespace boost;
int main(void)
{
timer t;
std::cout << "可度量最大时间:"
<< t.elapsed_max() / 3600 << "小时" << std::endl;
std::cout << "可度量最小时间:"
<< t.elapsed_min() << "秒" << std::endl;
std::cout << "已流逝时间:"
<< t.elapsed() << "秒" << std::endl;
return 0;
}
~/Boost/timer$a.out
可度量最大时间:2.56205e+09小时
可度量最小时间:1e-06秒
已流逝时间:0.000217秒
2.progress_timer类
progress_timer类派生于timer类,会在析构时自动输出时间,省去手动调用elapsed()
头文件: #include <boost/progress.hpp>
名字空间:using namespace boost;
class progress_timer:public timer, noncopyable
{
public:
explicit progress_timer();
progress_timer(std::ostream & os);
~progress_timer();
};
progress_timer类,在析构时允许输出定向I/O流,默认是std::cout,可以用其他标准库输出流(ofstream,ostringstream)替换,或者cout.rdbuf()重定向cout的输出。
8 #include <iostream>
9 #include <boost/progress.hpp>
10 using namespace boost;
11
12 int main(void){
13 progress_timer t;
14 int i = 8379016;
15 while (i > 0)
16 {
17 std::cout << i * i << std::endl;
18 i = i - 20;
19 }
20 return 0;
21 }
~/boost/a.out
55696
46656
38416
30976
24336
18496
13456
9216
5776
3136
1296
256
1.50 s
3.progress_display类
描述:该类可以在控制台上显示程序执行进度
头文件:#include <booost/progress.hpp>
名字空间: using namespace boost;
class progress_diaplay:noncopyable
{
public:
progress_diaplay (unsigned long expect_count); // 构造函数
progress_diaplay (unsigned long expect_count,
std::ostream& os,
const std::string & s1 = "\n",
const std::string & s2 = "\n",
const std::string & s3 = "\n"); // 构造函数
void restart(unsigned long expect_count); // 重新开始计时
unsigned long operator += (unsigned long increment); // 累加进度(按一定值)
unsigned long operator ++(); // 按一累加
unsigned long count() const; // 返回当前的计数
unsigned long expect_count() const; //
};
progress_diaplay无法把进度显示输出与程序的输出分离。因为他的输出都指向标准输出
#include <iostream>
#include <boost/progress.hpp>
#include <vector>
using namespace boost;
using namespace std;
int main(void){
vector <int> vec_num;
for (int i = 0; i < 50; ++i)
{
vec_num.push_back(i);
}
// 定义时间进度类
progress_display pd(vec_num.size());
for (int i = 0; i < vec_num.size(); ++i)
{
++pd;
}
return 0;
}
~/Boost/timer$a.out
0% 10 20 30 40 50 60 70 80 90 100%
|----|----|----|----|----|----|----|----|----|----|
***************************************************
---- 持续更新