boost之timer,progress_timer,progress_display的介绍及使用
boost計時器相關(guān)的類有三個:boost::timer boost::progress_timer(繼承自boost::timer) boost::progress_display
boost::timer?
頭文件:<boost/timer.hpp>
在定義的時候就開始計時,需要截止的時候調(diào)用boost::timer::elapsed()獲取從計時開始到目前為止的秒數(shù)。如果需要重新計時,可以使用boost::timer::restart()方法。
boost::progress_timer?
頭文件:<boost/progress.hpp>
boost::progress_timer繼承自boost::timer,使用更簡單, 增加的一點功能就是在析構(gòu)的時候自動輸出所經(jīng)過的秒數(shù),保留兩位小數(shù)。可以利用塊代碼中臨時變量的析構(gòu)特性,計算一個代碼塊中的執(zhí)行時間。
boost::progress_display?
頭文件:<boost/progress.hpp>
boost::progress構(gòu)造函數(shù)中需要一個expected_count整數(shù)作為進(jìn)度條達(dá)到100%時的最大數(shù)。?
然后這個類重載了operator+= operator++用來增加進(jìn)度條的大小。成員函數(shù)count()返回當(dāng)前的計數(shù),當(dāng)計數(shù)達(dá)到expected_count()時,進(jìn)度達(dá)到100%.
我們以boost::progress_timer 的使用為例
實驗如圖:
代碼如下:
#include <cstdio> #include <iostream> #include <boost/progress.hpp>using namespace std; using namespace boost;int main() {{cout<<"執(zhí)行10億次的加法耗時"<<endl;progress_timer t;int sum = 1; for (int i=0;i<1000000000;i++){sum += i;}cout<<"result:"<<sum<<endl;// 超過作用域時,變量析構(gòu),自動打印出時間}getchar();return 0; }?
總結(jié)
以上是生活随笔為你收集整理的boost之timer,progress_timer,progress_display的介绍及使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: boost的multi_index性能测
- 下一篇: Ubuntu增加一个用户并给普通用户赋予