C Boost庫學習之timer庫

2021-08-27 15:44:19 字數 2297 閱讀 9769

1.timer庫概述

①命名空間timer

②計時器類中用到的格式

2.類cpu_timer

①類定義

②使用例子

3.類auto_cpu_timer

①類定義

②使用例子

timer是乙個很小的庫,提供簡易的計時功能,對了解程式執行所需的時間在測試和生產環境中都很有用。

舊版本的計時器已經被棄用了,取而代之的是是更符合當前boost實踐的cpu計時器cpu_timer和auto_cpu_timer。所有元件都在命名空間timer中,如下:

①命名空間timer ^

//標頭檔案:

namespace boost

; //預設精度為6

const

int default_places = 6;

//將時間轉化為指定精度,或指定精度與格式的字串,單位為秒

std::string format(const cpu_times& times, short places, const

std::string& format);

std::string format(const cpu_times& times, short places = default_places);

}}

②計時器類中用到的格式 ^

格式字串

形式%w

times.wall

%utimes.user

%stimes.system

%ttimes.user + times.system

%ptimes.wall,times.user + times.system

cpu_timer物件測量經過時間,使用者程序時間和系統處理時間。

①cpu_timer類定義 ^

class cpu_timer

;

注:resume()只是接著計時,並不是重新開始計時,如果想重新開始計時應該呼叫start()

②使用例子 ^

#include

#include

using

namespace boost;

using

std::cout;

using

std::endl;

int main()

t.stop();

cout

//輸出為真

if (t.is_stopped())

cout

<< "stop()之後resume()之前:為真"

<< endl;

else

cout

<< "stop()之後resume()之前:為假"

<< endl;

t.resume();

//輸出為假

if (t.is_stopped())

cout

<< "stop()和resume()之後:為真"

<< endl;

else

cout

<< "stop()和resume()之後:為假"

<< endl;

for (int i = 0; i != 200; ++i)

}t.stop();

cout

<< t.format(3,"%w"); //以3精度輸出經過時間

}

auto_cpu_timer繼承至cpu_timer,是乙個更為精細的cpu_timer,物件建立後即開始計時,當它被銷毀時能自動的報告花費的時間。

①auto_cpu_timer類定義 ^

class auto_cpu_timer : public cpu_timer

;

②使用例子 ^

#include

#include

using

namespace boost;

using

std::cout;

using

std::endl;

int main()

for (int i = 0; i != 200; ++i)

cout

<< "精度:"

<< t.places() << endl; //輸出:4

cout

<< "格式:"

<< t.format_string() << endl; //輸出:%w

cout

}

C Boost 庫文件索引

2.0 字串和文字處理 string and text processing 2.1 容器 containers 2.2 迭代器 iterators 2.3 演算法 algorithms 2.4 函式物件和高階程式設計 function objects and higher order progra...

C Boost 庫文件索引

3 boost中已廢除的庫 對一些庫的文件的其他可選檔案格式 2.0 字串和文字處理 string and text processing 2.1 容器 containers 2.2 迭代器 iterators 2.3 演算法 algorithms 2.4 函式物件和高階程式設計 function ...

C Boost 庫編譯技巧總結

fishing pan 這是很早之前的乙個工作了,當時需要修改和重新編譯其他實驗室的軟體的原始碼,所以需要自己編譯boost。因此對於如何編譯boost一點簡單了解,這裡分享給大家。我自己沒有使用它做開發哦!如果開發方面的求助,我就愛莫能助了!在這裡,我是採用vs2013進行編譯的。因為我需要的是x...