缘起
最近做遥感影像融合的GWPCA方法,在带宽比较大的时候速度太慢了,需要有个进度条指示一下,然后我去找进度条的库,发现github上面的C/C++的相应的库似乎没有能在VS下跑的,自己花了点时间写了一个。
效果
实现
大概需要考虑这样几个要素
已完成的百分比
执行速度
已执行的时间
剩余时间
另外进度条的引入不能破坏已有的执行结构,最好和Python的tqdm库类似,通过 start , update 等函数来完成整个进度条,因此对于C语言来说,需要一个定时器,定期将进度条进行重绘(不可能更新一次就重绘一次),因此整个进度条就包含了两个类,一个是进度条类,一个是定时器类。另外需要考虑线程安全的问题。
// Progress.hpp
#pragma once
#include
#include
#include
#include
#include "Timer.hpp"
using namespace std::chrono;
class ProgressBar
{
protected:
// 进度条的长度(不包含前后缀)
unsigned int ncols;
// 已完成的数量
std::atomic finishedNum;
// 上次的已完成数量
unsigned int lastNum;
// 总数
unsigned int totalNum;
// 进度条长度与百分比之间的系数
double colsRatio;
// 开始时间
steady_clock::time_point beginTime;
// 上次重绘的时间
steady_clock::time_point lastTime;
// 重绘周期
milliseconds interval;
Timer timer;
public:
ProgressBar(unsigned int totalNum, milliseconds interval) : totalNum(totalNum), interval(