C++ chrono 庫中的 steady_clock 和 system_clock


C++11 中提供了一個計時的標准庫 <chrono>;
里面有三種時鍾 clock: steady_clock, system_clock 和 high_resolution_clock;

區別

  • steady_clock 是單調的時鍾,相當於教練手中的秒表;只會增長,適合用於記錄程序耗時
  • system_clock 是系統的時鍾;因為系統的時鍾可以修改;甚至可以網絡對時; 所以用系統時間計算時間差可能不准。
  • high_resolution_clock 是當前系統能夠提供的最高精度的時鍾;它也是不可以修改的。相當於 steady_clock 的高精度版本。

在 參考鏈接[2] 中提供了代碼,可以查看自己的機器上三種時鍾的時間精度。

// copied from http://www.informit.com/articles/article.aspx?p=1881386&seqNum=2;
// Author: Nicolai M. Josuttis

#include <chrono>
#include <iostream>
#include <iomanip>

template <typename C>
void printClockData ()
{
    using namespace std;

    cout << "- precision: ";
    // if time unit is less or equal one millisecond
    typedef typename C::period P;// type of time unit
    if (ratio_less_equal<P,milli>::value) {
       // convert to and print as milliseconds
       typedef typename ratio_multiply<P,kilo>::type TT;
       cout << fixed << double(TT::num)/TT::den
            << " milliseconds" << endl;
    }
    else {
        // print as seconds
        cout << fixed << double(P::num)/P::den << " seconds" << endl;
    }
    cout << "- is_steady: " << boolalpha << C::is_steady << endl;
}

int main()
{
    std::cout << "system_clock: " << std::endl;
    printClockData<std::chrono::system_clock>();
    std::cout << "\nhigh_resolution_clock: " << std::endl;
    printClockData<std::chrono::high_resolution_clock>();
    std::cout << "\nsteady_clock: " << std::endl;
    printClockData<std::chrono::steady_clock>();

#ifdef _WIN32
    system("pause");
#endif
    return 0;
}
system_clock:
- precision: 0.000100 milliseconds
- is_steady: false

high_resolution_clock:
- precision: 0.000001 milliseconds
- is_steady: true

steady_clock:
- precision: 0.000001 milliseconds
- is_steady: true

建議

以下是 stackoverflow 上一個大佬給出的建議difference between steady clocl vs system clock

  1. 盡量不要使用 count() 方法
  2. 盡量不要使用 time_since_epoch()

理由是: 提供了類型安全的機制防止用戶進行單位換算的時候出錯;但是這兩個函數是例外的,起到“緊急出口的作用”,

Such emergencies arise when (for example) the committee neglects to give you all the tools you need to get the job done (such as I/O) for the types, or such as the need to interface with some other timing API via integers

在I/O 或者與其他 通過整數傳參數的時間函數接口中使用。

使用方法

例子:一個用來測試代碼段運行時間的宏


#include <chrono>

#define TIMERSTART(tag)  auto tag##_start = std::chrono::steady_clock::now(),tag##_end = tag##_start
#define TIMEREND(tag)  tag##_end =  std::chrono::steady_clock::now()
#define DURATION_s(tag) printf("%s costs %d s\n",#tag,std::chrono::duration_cast<std::chrono::seconds>(tag##_end - tag##_start).count())
#define DURATION_ms(tag) printf("%s costs %d ms\n",#tag,std::chrono::duration_cast<std::chrono::milliseconds>(tag##_end - tag##_start).count());
#define DURATION_us(tag) printf("%s costs %d us\n",#tag,std::chrono::duration_cast<std::chrono::microseconds>(tag##_end - tag##_start).count());
#define DURATION_ns(tag) printf("%s costs %d ns\n",#tag,std::chrono::duration_cast<std::chrono::nanoseconds>(tag##_end - tag##_start).count());


// usage:
//   TIMERSTART(for_loop);
//   for (int i = 0; i < 100000; i++)
//   {
//       i*i;
//   }
//   TIMEREND(for_loop);
//   DURATION_ms(for_loop);

參考

  1. https://stackoverflow.com/questions/31552193/difference-between-steady-clock-vs-system-clock
  2. C++ Stardard Library:A Tutorial and Reference, 2nd Edition


免責聲明!

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



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