1. 程式人生 > 程式設計 >用C++實現一個命令列進度條的示例程式碼

用C++實現一個命令列進度條的示例程式碼

緣起

最近做遙感影像融合的GWPCA方法,在頻寬比較大的時候速度太慢了,需要有個進度條指示一下,然後我去找進度條的庫,發現github上面的C/C++的相應的庫似乎沒有能在VS下跑的,自己花了點時間寫了一個。

效果

用C++實現一個命令列進度條的示例程式碼

實現

大概需要考慮這樣幾個要素

  • 已完成的百分比
  • 執行速度
  • 已執行的時間
  • 剩餘時間

另外進度條的引入不能破壞已有的執行結構,最好和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;
  // 重繪週期
	milliseconds interval;
	Timer timer;
public:
	ProgressBar(unsigned int totalNum,milliseconds interval) : totalNum(totalNum),interval(interval),finishedNum(0),lastNum(0),ncols(80),colsRatio(0.8) {}
  // 開始
	void start();
  // 完成
	void finish();
  // 更新
	void update() { return this->update(1); }
  // 一次更新多個數量
	void update(unsigned int num) { this->finishedNum += num; }
  // 獲取進度條長度
	unsigned int getCols() { return this->ncols; }
  // 設定進度條長度
	void setCols(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->interval.count(),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 present = (100.0 * tmpFinished) / this->totalNum;
	// 列印百分數
	std::cout << std::setprecision(1) << std::fixed << present << "%|";
	// 計算應該繪製多少=符號
	int barWidth = present * this->colsRatio;
	// 列印已完成和未完成進度條
	std::cout << std::setw(barWidth) << std::setfill('=') << "=";
	std::cout << std::setw(this->ncols - barWidth) << std::setfill(' ') << "|";

	// 列印速度
	std::cout << std::setprecision(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;
	}
	else {
		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(false)
	{}

	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 == false)
			return;

		// start async timer,launch thread and wait in that thread
		_expired = false;
		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::milliseconds(interval));
				task();
			}

			{
				// timer be stopped,update the condition variable expired and wake main thread
				std::lock_guard<std::mutex> locker(_mutex);
				_expired = true;
				_expired_cond.notify_one();
			}
		}).detach();
	}

	void startOnce(int delay,std::function<void()> task)
	{
		std::thread([delay,task]() {
			std::this_thread::sleep_for(std::chrono::milliseconds(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; });

			// reset the timer
			if (_expired == true)
				_try_to_expire = false;
		}
	}

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++ 命令列進度條內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!