REF:boost庫使用—計時器類timer, 19.12
timer是一個很小的庫,提供簡單的時間度量和進度顯示功能,也可用於性能測試等計時任務。timer庫包含三個組件:計時器類timer、progress_timer和進度指示類progress_display。
計時器類timer
需包含頭文件 #include <boost/timer.hpp>
示例
#include <iostream> #include <windows.h> #include <boost/timer.hpp> using namespace std; int main() { //開始計時 boost::timer t; //可度量最大時間 cout << "max timespan:" << t.elapsed_max() / 3600 << "h" << endl; //可度量最小時間 cout << "min timespac:" << t.elapsed_min() << endl; //第一次計時 cout << "now time elapsed:" << t.elapsed() << endl; Sleep(1000); //第二次計時 cout << "now time elapsed:" << t.elapsed() << endl; return 0; }
計時器類progress_timer
包含頭文件:
#include <boost/progress.hpp>
說明:
progress_timer繼承於timer,所以timer的函數也可以調用;
progress_timer計時是析構后自動輸出;也可以多個計時,需要用{}包含
示例
#include <iostream> #include <windows.h> #include <boost/progress.hpp> using namespace std; int main() { { //多個計時 { //第一個計時 //啟動計時器 boost::progress_timer tt; Sleep(100); cout << "one progress time elapsed:" << tt.elapsed() << endl; } //第二個計時 { boost::progress_timer tt; Sleep(100); //輸出計時器數 cout << "two progress time elapsed:" << tt.elapsed() << endl; //{}結束代表析構函數 } } system("pause"); return 0; }
進度指示類progress_display
包含頭文件:
#include <boost/progress.hpp>
不太合理,不建議使用。
示例
#include <iostream> #include <windows.h> #include <boost/timer.hpp> #include <vector> #include <boost/progress.hpp> using namespace std; vector<string>v(100); //申明基數大小 boost::progress_display pd(v.size()); for (auto it = v.begin(); it != v.end(); it++) { Sleep(100); //正常使用,沒有其他輸出 //++pd; cout << "hello progress_display" << endl; //輸出會打亂格式 pd.restart(v.size()); pd += (it-v.begin()+1); } system("pause"); return 0; }