boost.timer:一個優秀的計時類庫


在編寫程序的時候很多種情況下我們都需要計時,比如我們在測試軟件的性能時,或者一個和時間有關的軟件時 boost.timer 就特別有用 ,boost.timer 提供兩個類 cpu_timer auto_cpu_timer ,它們都包含在 boost::timer 命名空間內。

auto_cpu_timer

廢話不多說先看一個例子:


//  if on win32 this make the program auto link to dynme library  
#define BOOST_ALL_DYN_LINK   
#include <boost/timer/timer.hpp>  
  
void func(some arguments)  
{  
    ...  
}  
  
int main( void)  
{  
     //  a boost.timer class auto_cpu_time  
    boost::timer::auto_cpu_time   
    timer(“%w clock time,%t totle program time(%p%)”);  
  
     for ( int i =  0;i <  10000;i++)  
    {  
        ...  
        func(some arguments);  
        ...  
    }
}
      程序運行結果
0.318385 clock time, 0.312002 totle program time( 98.0%) 

      我們看到代碼中僅僅聲明了一個boost::timer::auto_cpu_timer類,for循環的運行時間(包括clock time即世界時間,和cpu time即程序在cpu中運行的時間)就被計算出來了,究其原因,我們大概可以猜想到auto_cpu_timer在構造階段調用了計時相關的函數,在析構階段結束計時,並以一定的格式輸出結果,這其中有一個細節就是:定義在區塊中的自動變量(auto value)都會在離開區塊是時動析構,這里的析構對於內建的類型如int char等就是釋放棧空間,對於用戶定義類型就是調用析構函數后釋放空間。上述過程是C++語言規定的,它由C++編譯器強制保證。

正如auto_cpu_timer的名字一樣,我們只需要簡單的定義一個auto_cpu_timer變量便可方便的計時了,auto_cpu_timer有六個構造函數:

explicit auto_cpu_timer( short places = default_places);      //  #1  
    auto_cpu_timer( short places,  const std:: string& format);     //  #2  
     explicit auto_cpu_timer( const std:: string& format);          //  #3  
    auto_cpu_timer(std::ostream& os,  short places,  
                const std:: string& format)                    //  #4  
     explicit auto_cpu_timer(std::ostream&os,  
                         short places = default_places);      //  #5  
    auto_cpu_timer(std::ostream& os,  const std:: string& format)  //  #6 

 

os

其中的std::ostream指向一個輸出流這就意味着可以將輸出結果定位到任意輸出流了如標准輸出,日志文件等等

format

其中的std::string 是一個格式化字符串就如printf函數中的格式化字符串一樣不過具體的格式如下:

 

%w

wall clock time指的是世界時間比如鍾表的時間

%u

user time指的是用戶代碼執行時間 即程序在用戶態時間

%s

system time指的是系統代碼執行時間即程序在內核中的時間

%t

user time + system time

%p

he peacetion of the totle time in wall time程序總時間在世界時間中的百分比(由於cpu的線程切換,totle time 總是不大於wall time

 

places

places指的是保留的秒數小數點的位數,默認的是8位,超過8為的會被忽略並置為8位小於0的也會被忽略,並會被置為0

cup_timer

事實上auto_cpu_timer繼承於cpu_time,但是我們在簡單的情況下都是運用auto_cpu_timer的自動機制的,如果情況復雜就應該用cup_time

cup_time有三個動作:starvoid)、stopvoid)和resumevoid)分別負責從當前時間點啟動計時、停止計時,和從上一個計時啟動點重新開始計時。

有三個狀態函數is_stopedvoid)、elapsedvoid)和format..., is_stop指示計時是否已經停止,elapsed返回當前計時以用時間但是不停止計時,format返回當前計時以用時間的字符串形式,不停止計時。

cup_timer的聲明代碼大致如下:

 

 

class BOOST_TIMER_DECL cpu_timer  
    {  
       public:  
      
         //   constructor  
        cpu_timer() { start(); }  
         //   observers  
         bool        is_stopped()  const  {  return m_is_stopped; }  
        cpu_times  elapsed()  const;   //  does not stop()  
        std:: string   format( short places,  const std:: string& format)  const;  
        std:: string   format( short places = default_places)  const  
         //   actions  
         void          start();  
         void          stop();  
         void          resume();   
      
       private:  
        cpu_times     m_times;  
         bool          m_is_stopped;  
    }; 

 其中保存時間的結構體cpu_times定義如下:

 

typedef boost::int_least64_t nanosecond_type;  
struct cpu_times  
{
    nanosecond_type wall;  
    nanosecond_type user;  
    nanosecond_type system;  
      
     void clear() { wall = user = system = 0LL;}
}; 

其中的boost::int_least64_t 事實上是__int64類型的

下面代碼展示了cup_time的基本用法:

#define BOOST_ALL_DYN_LINK  
#include <boost/timer/timer.hpp>  
void fun( void)  
{  
     for ( int i =  0;i <  1000;i++);  
}  
int main( void)  
{  
    boost::timer::cpu_timer timer;  
     for ( int i =  0;i <  100000;i++)  
        fun();  
    std::cout << timer.format( 5, " %ws wall time,%ts totle time\n ");  
      
    boost::timer::cpu_times time = timer.elapsed();  
    std::cout <<  " wall time is : " << time.wall <<  " ns( "   
              << time.wall/ 1000000000. 0L <<  " s) " << std::endl;  
     return  0

}

 

boost.timer 事實上實現起來並不困難,只是由於跨平台,需要調用不同的系統 API 來得到精密的時間而已,運用 boost.timer 我們可以在大多數的 Linux/Unix 系統和 win32 系統上運行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM