心期
⽤C++实现⼀个命令⾏进度条的⽰例代码
缘起
最近做遥感影像融合的GWPCA⽅法,在带宽⽐较⼤的时候速度太慢了,需要有个进度条指⽰⼀下,然后我去找进度条的库,发现github上⾯的C/C++的相应的库似乎没有能在VS下跑的,⾃⼰花了点时间写了⼀个。
效果
实现
⼤概需要考虑这样⼏个要素
已完成的百分⽐
执⾏速度
已执⾏的时间
剩余时间
另外进度条的引⼊不能破坏已有的执⾏结构,最好和Python的tqdm库类似,通过start , update等函数来完成整个进度条,因此对于C语⾔来说,需要⼀个定时器,定期将进度条进⾏重绘(不可能更新⼀次就重绘⼀次),因此整个进度条就包含了两个类,⼀个是进度条类,⼀个是定时器类。另外需要考虑线程安全的问题。
// Progress.hpp
#pragma once
#include <ctime>
#include <chrono>
#include <iostream>
#include <iomanip>
#include "Timer.hpp"
using namespace std::chrono;
class ProgressBar
{
protected:
// 进度条的长度(不包含前后缀)
unsigned int ncols;
// 已完成的数量
std::atomic<unsigned int> finishedNum;
/
/ 上次的已完成数量
unsigned int lastNum;
// 总数
unsigned int totalNum;
// 进度条长度与百分⽐之间的系数
double colsRatio;
// 开始时间
steady_clock::time_point beginTime;
// 上次重绘的时间
steady_clock::time_point lastTime;
// 重绘周期
将军蟹milliconds interval;
李白清平调Timer timer;
public:
ProgressBar(unsigned int totalNum, milliconds interval) : totalNum(totalNum), interval(interval), finishedNum(0), lastNum(0), ncols(80), colsRatio(0.8) {}
// 开始
void start();
word页眉横线
// 完成
void finish();
// 更新
void update() { return this->update(1); }
// ⼀次更新多个数量
void update(unsigned int num) { this->finishedNum += num; }
// 获取进度条长度
unsigned int getCols() { return this->ncols; }
红毛城// 设置进度条长度
void tCols(unsigned int ncols) { this->ncols = ncols; this->colsRatio = ncols / 100; }
// 重绘
void show();
};
void ProgressBar::start() {
// 记录开始时间,并初始化定时器
this->beginTime = steady_clock::now();
this->lastTime = this->beginTime;
// 定时器⽤于定时调⽤重绘函数
this->timer.start(this-&unt(), std::bind(&ProgressBar::show, this));
// 重绘函数
void ProgressBar::show() {
// 清除上次的绘制内容
std::cout << "\r";
// 记录重绘的时间点
steady_clock::time_point now = steady_clock::now();
// 获取已完成的数量
unsigned int tmpFinished = this->finishedNum.load();
/
/ 获取与开始时间和上次重绘时间的时间间隔
auto timeFromStart = now - this->beginTime;
auto timeFromLast = now - this->lastTime;
// 这次完成的数量
unsigned int gap = tmpFinished - this->lastNum;
// 计算速度
四川酸菜鱼double rate = gap / duration<double>(timeFromLast).count();
// 应显⽰的百分数
double prent = (100.0 * tmpFinished) / this->totalNum;
// 打印百分数
std::cout << std::tprecision(1) << std::fixed << prent << "%|"; // 计算应该绘制多少=符号
int barWidth = prent * this->colsRatio;
// 打印已完成和未完成进度条
std::cout << std::tw(barWidth) << std::tfill('=') << "=";
std::cout << std::tw(this->ncols - barWidth) << std::tfill(' ') << "|"; // 打印速度
std::cout << std::tprecision(1) << std::fixed << rate << "op/s|";
// 之后的两部分内容分别为打印已过的时间和剩余时间
int timeFromStartCount = duration<double>(timeFromStart).count(); std::time_t tfs = timeFromStartCount;
tm tmfs;
gmtime_s(&tmfs, &tfs);
std::cout << std::put_time(&tmfs, "%X") << "|";
int timeLast;
if (rate != 0) {
// 剩余时间的估计是⽤这次的速度和未完成的数量进⾏估计
timeLast = (this->totalNum - tmpFinished) / rate;
}
el {
timeLast = INT_MAX;
}
if ((this->totalNum - tmpFinished) == 0) {
timeLast = 0;
}
std::time_t tl = timeLast;
tm tml;
gmtime_s(&tml, &tl);
std::cout << std::put_time(&tml, "%X");
this->lastNum = tmpFinished;
this->lastTime = now;
}
void ProgressBar::finish() {
// 停⽌定时器
this->timer.stop();
std::cout << std::endl;
陶瓷定做}
/
/ Timer.hpp
#pragma once
#include <functional>
#include <chrono>
#include <thread>
#include <atomic>
#include <memory>
#include <mutex>
#include <condition_variable>
using namespace std::chrono;
class Timer
{
public:
Timer() : _expired(true), _try_to_expire(fal)
{}
Timer(const Timer& timer)
{
_expired = timer._expired.load();
_try_to_expire = timer._try_to_expire.load();
}
~Timer()
{
stop();
}
void start(int interval, std::function<void()> task)
// is started, do not start again
if (_expired == fal)
return;
// start async timer, launch thread and wait in that thread
_expired = fal;
std::thread([this, interval, task]() {
while (!_try_to_expire)
{
/
/ sleep every interval and do the task again and again until times up
std::this_thread::sleep_for(std::chrono::milliconds(interval));
task();
}
{
// timer be stopped, update the condition variable expired and wake main thread
std::lock_guard<std::mutex> locker(_mutex);
_expired = true;
_ify_one();
}
荆州美食}).detach();
}
void startOnce(int delay, std::function<void()> task)
{
std::thread([delay, task]() {
std::this_thread::sleep_for(std::chrono::milliconds(delay));
task();
}).detach();
}
void stop()
{
// do not stop again
if (_expired)
return;
if (_try_to_expire)
return;
// wait until timer
_try_to_expire = true; // change this bool value to make timer while loop stop
{
std::unique_lock<std::mutex> locker(_mutex);
_expired_cond.wait(locker, [this] {return _expired == true; });
// ret the timer
if (_expired == true)
_try_to_expire = fal;
}
}
private:
std::atomic<bool> _expired; // timer stopped status
std::atomic<bool> _try_to_expire; // timer is in stop process
std::mutex _mutex;
std::condition_variable _expired_cond;
};
定时器类是直接copy了⼀篇
可以增加的功能
读者可以⾃⾏调整⼀下结构,增加⼀些有意思的⼩功能,⽐如说⽤于表⽰完成内容的符号可以替换成⼤家喜欢的符号,或者加个颜⾊什么的
还有⼀些复杂的功能,⽐如说分组进度条等,不过这个由于我没这⽅⾯的需求,因此就没研究了,读者可以⾃⾏研究
到此这篇关于⽤C++实现⼀个命令⾏进度条的⽰例代码的⽂章就介绍到这了,更多相关C++ 命令⾏进度条内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!