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