最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++11計時器之chrono庫簡介

 更新時間:2023年12月14日 10:36:34   作者:森明幫大于黑虎幫  
C++11有了chrono庫,可以在不同系統(tǒng)中很容易的實(shí)現(xiàn)定時功能,要使用chrono庫,需要#include,其所有實(shí)現(xiàn)均在std::chrono namespace下,本文給大家介紹C++11計時器:chrono庫介紹,感興趣的朋友一起看看吧

C++11計時器:chrono庫介紹

C++11有了chrono庫,可以在不同系統(tǒng)中很容易的實(shí)現(xiàn)定時功能。

要使用chrono庫,需要#include,其所有實(shí)現(xiàn)均在std::chrono namespace下。注意標(biāo)準(zhǔn)庫里面的每個命名空間代表了一個獨(dú)立的概念。

chrono是一個模版庫,使用簡單,功能強(qiáng)大,只需要理解三個概念:duration、time_point、clock

一 、時鐘-CLOCK

chrono庫定義了三種不同的時鐘:

 std::chrono::system_clock:  依據(jù)系統(tǒng)的當(dāng)前時間 (不穩(wěn)定)
 std::chrono::steady_clock:  以統(tǒng)一的速率運(yùn)行(不能被調(diào)整)
 std::chrono::high_resolution_clock: 提供最高精度的計時周期(可能是steady_clock或者system_clock的typedef)

二、這三個時鐘有什么區(qū)別呢?

system_clock就類似Windows系統(tǒng)右下角那個時鐘,是系統(tǒng)時間。明顯那個時鐘是可以亂設(shè)置的。明明是早上10點(diǎn),卻可以設(shè)置成下午3點(diǎn)。

steady_clock則針對system_clock可以隨意設(shè)置這個缺陷而提出來的,他表示時鐘是不能設(shè)置的。

high_resolution_clock則是一個高分辨率時鐘。

這三個時鐘類都提供了一個靜態(tài)成員函數(shù)now()用于獲取當(dāng)前時間,該函數(shù)的返回值是一個time_point類型,

system_clock除了now()函數(shù)外,還提供了to_time_t()靜態(tài)成員函數(shù)。用于將系統(tǒng)時間轉(zhuǎn)換成熟悉的std::time_t類型,得到了time_t類型的值,在使用ctime()函數(shù)將時間轉(zhuǎn)換成字符串格式,就可以很方便地打印當(dāng)前時間了。

#include<iostream>
#include<vector>
#include<string>
#include<ctime>//將時間格式的數(shù)據(jù)轉(zhuǎn)換成字符串
#include<chrono>
using namespace std::chrono;
using namespace std;
int main()
{
    //獲取系統(tǒng)的當(dāng)前時間
    auto t = system_clock::now();
    //將獲取的時間轉(zhuǎn)換成time_t類型
    auto tNow = system_clock::to_time_t(t);
    //ctime()函數(shù)將time_t類型的時間轉(zhuǎn)化成字符串格式,這個字符串自帶換行符
    string str_time = std::ctime(&tNow);
    cout<<str_time;
    return 0;
}

三、持續(xù)的時間 - duration

td::chrono::duration<int,ratio<60,1>> ,表示持續(xù)的一段時間,這段時間的單位是由ratio<60,1>決定的,int表示這段時間的值的類型,函數(shù)返回的類型還是一個時間段duration

std::chrono::duration<double,ratio<60,1>>

由于各種時間段(duration)表示不同,chrono庫提供了duration_cast類型轉(zhuǎn)換函數(shù)。

duration_cast用于將duration進(jìn)行轉(zhuǎn)換成另一個類型的duration。

duration還有一個成員函數(shù)count(),用來表示這一段時間的長度

#include<iostream>
#include<string.h>
#include<chrono>
using namespace std::chrono;
using namespace std;
int main()
{
    auto start = std::chrono::steady_clock::now();
    for(int i=0;i<100;i++)
        cout<<"nice"<<endl;
    auto end = std::chrono::steady_clock::now();
    auto tt = std::chrono::duration_cast<microseconds>(end - start);
    cout<<"程序用時="<<tt.count()<<"微秒"<<endl;
    return 0;
}

四、時間點(diǎn) - time_point

std::chrono::time_point 表示一個具體時間,如上個世紀(jì)80年代、你的生日、今天下午、火車出發(fā)時間等,只要它能用計算機(jī)時鐘表示。鑒于我們使用時間的情景不同,這個time point具體到什么程度,由選用的單位決定。一個time point必須有一個clock計時

設(shè)置一個時間點(diǎn):

std::time_point<clock類型> 時間點(diǎn)名字

//設(shè)置一個高精度時間點(diǎn)
    std::time_point<high_resolution_clock> high_resolution_clock::now();
//設(shè)置系統(tǒng)時鐘
	std::chrono::time_point<std::chrono::system_clock> now=std::chrono::system_clock::now();

另一個實(shí)例:

#define _CRT_SECURE_NO_WARNINGS //localtime()需要這個宏
#include<iostream>
#include<chrono>
#include<vector>
#include<string>
#include<algorithm>
//#include<stdio.h>
#include<iomanip> //put_time需要的頭文件
#include<sstream>
    // template <class _Rep>
    // struct treat_as_floating_point : is_floating_point<_Rep> {}; // tests for floating-point type
    // template <class _Rep>
    // _INLINE_VAR constexpr bool treat_as_floating_point_v = treat_as_floating_point<_Rep>::value;
    // // STRUCT TEMPLATE duration_values
    // template <class _Rep>
    // struct duration_values { // gets arithmetic properties of a type
    //     _NODISCARD static constexpr _Rep zero() noexcept {
    //         // get zero value
    //         return _Rep(0);
    //     }
    //     _NODISCARD static constexpr _Rep(min)() noexcept {
    //         // get smallest value
    //         return numeric_limits<_Rep>::lowest();
    //     }
    //     _NODISCARD static constexpr _Rep(max)() noexcept {
    //         // get largest value
    //         return (numeric_limits<_Rep>::max)();
    //     }
    // };
//時間長度
void Func1(){
    //chrono重載了各種運(yùn)算符
    std::chrono::hours             c1(1);                //1小時
    std::chrono::minutes           c2(60);               //60分鐘
    std::chrono::seconds           c3(60*60);            //60*60s
    std::chrono::milliseconds      c4(60*60*1000);       //60*60*1000毫秒
    std::chrono::microseconds      c5(60*60*1000*1000);  //微秒 溢出
    std::chrono::nanoseconds       c6(60*1000*1000*1000);//納秒 溢出
    if(c1==c2){
        std::cout<<"c1==c2"<<std::endl;
    }
    if(c1==c3){
        std::cout<<"c1==c3"<<std::endl;
    }
    if(c2==c3){
        std::cout<<"c2==c3"<<std::endl;
    }
    //獲取時鐘周期的值,返回的是int整數(shù)
    std::cout<<"c1= "<<c1.count()<<std::endl;
    std::cout<<"c2= "<<c2.count()<<std::endl;
    std::cout<<"c3= "<<c3.count()<<std::endl;
    std::cout<<"c4= "<<c4.count()<<std::endl;
    std::chrono::seconds             c7(1);               //1秒
    std::chrono::milliseconds        c8(1*1000);          //1000毫秒;1s
    std::chrono::microseconds        c9(1*1000*1000);     //1000*1000微秒 1s
    std::chrono::nanoseconds         c10(1*1000*1000*1000);//1000*1000*1000納秒 1s;
    std::cout<<c7.count()<<std::endl;
    std::cout<<c8.count()<<std::endl;
    std::cout<<c9.count()<<std::endl;
    std::cout<<c10.count()<<std::endl;
}
//系統(tǒng)時間
/**
 * @brief 
 * 
 * system_clock 類支持了對系統(tǒng)時鐘的訪問, 提供了三個靜態(tài)成員函數(shù):
返回當(dāng)前時間的時間點(diǎn)。
static std::chrono::time_point<std::chrono::system_clock> now() noexcept;
將時間點(diǎn) time_point 類型轉(zhuǎn)換為 std::time_t 類型。
static std::time_t to_time_t( const time_point& t ) noexcept;
將 std::time_t 類型轉(zhuǎn)換為時間點(diǎn) time_point 類型。
static std::chrono::system_clock::time_point from_time_t( std::time_t t ) noexcept;
 */
void Func2(){
    //靜態(tài)成員函數(shù) static std::chrono::time_point<std::chrono::system_clock> now() noexcept
    //這些都可以簡寫用auto now=std::chrono::system_clock()::now()接收
    //1、靜態(tài)成員函數(shù) static std::chrono::system_clock::now()用來獲取系統(tǒng)時間,C++時間
    std::chrono::time_point<std::chrono::system_clock> now=std::chrono::system_clock::now();
    //2、靜態(tài)成員函數(shù) static std::chrono::system_clock::to_time_t()把C++系統(tǒng)時間轉(zhuǎn)換為time_t (utc時間)
    time_t t_now=std::chrono::system_clock::to_time_t(now);
    //3、std::localtime()函數(shù)把time_t時間轉(zhuǎn)換為本地時間(北京時間)
    // std::localtime()不是線程安全的,VS用localtime_t()代替,linux用local_time_r()代替
    //tm結(jié)構(gòu)體
    tm* tm_now=localtime(&t_now);
    //格式化輸出tm結(jié)構(gòu)體中的成員
    std::cout<<std::put_time(tm_now,"%Y-%m-%d %H:%M:%S")<<std::endl;
    std::cout<<std::put_time(tm_now,"%Y-%m-%d")<<std::endl;
    std::cout<<std::put_time(tm_now,"%H:%M:%S")<<std::endl;
    //但是通常C++一般不打印出時間,而是把時間存儲到一個字符串中
    std::stringstream ss;  //創(chuàng)建stringstream對象 ss,需要包含<sstream>頭文件
    ss<<std::put_time(tm_now,"%Y-%m-%d %H:%M:%S");
    std::string time_str=ss.str();
    std::cout<<time_str<<std::endl;
}   
// struct steady_clock { // wraps QueryPerformanceCounter
//         using rep                       = long long;
//         using period                    = nano;
//         using duration                  = nanoseconds;
//         using time_point                = chrono::time_point<steady_clock>;
//         static constexpr bool is_steady = true;
//         _NODISCARD static time_point now() noexcept { // get current time
//             const long long _Freq = _Query_perf_frequency(); // doesn't change after system boot
//             const long long _Ctr  = _Query_perf_counter();
//             static_assert(period::num == 1, "This assumes period::num == 1.");
//             // Instead of just having "(_Ctr * period::den) / _Freq",
//             // the algorithm below prevents overflow when _Ctr is sufficiently large.
//             // It assumes that _Freq * period::den does not overflow, which is currently true for nano period.
//             // It is not realistic for _Ctr to accumulate to large values from zero with this assumption,
//             // but the initial value of _Ctr could be large.
//             const long long _Whole = (_Ctr / _Freq) * period::den;
//             const long long _Part  = (_Ctr % _Freq) * period::den / _Freq;
//             return time_point(duration(_Whole + _Part));
//         }
//     };
//     using high_resolution_clock = steady_clock;
// } // namespace chrono
//計時器 steady_clock 類相當(dāng)于秒表,操作系統(tǒng)只要啟動就會進(jìn)行時間的累加,常用于耗時的統(tǒng)計(精確到納秒) 。
void Func3(){
    //靜態(tài)成員函數(shù)std::chrono::steady_clock::now()獲取時間的開始點(diǎn)
    std::chrono::time_point<std::chrono::steady_clock> start=std::chrono::steady_clock::now();
    //auto start=std::chrono::steady_clock::now();
    //執(zhí)行一些代碼,消耗時間
    std::vector<std::string> vec1{"banana","apple","pear"};
    std::for_each(vec1.begin(),vec1.end(),[&vec1](std::string str){
        std::cout<<str<<" ";
    });
    std::cout<<std::endl;
    //靜態(tài)成員函數(shù)std::chrono::steady_clock::now()獲取時間的結(jié)束點(diǎn)
    auto end=std::chrono::steady_clock::now();
    //計算消耗的時間,單位是納秒
    auto dt=end-start;
    std::cout<<"耗時: "<<dt.count()<<"納秒 ("<<(double)dt.count()/(1000*1000*1000)<<"秒) "<<std::endl;
}
int main(int argc,char* argv[]){
    Func1();
    Func2();
    Func3();
    return 0;
}

輸出結(jié)果:

PS D:\時間操作 chrono 庫\bin\Debug> .\main.exe
c1==c2
c1==c3
c2==c3
c1= 1
c2= 60
c3= 3600
c4= 3600000
1
1000
1000000
1000000000
2023-01-04 22:32:43
2023-01-04
22:32:43
2023-01-04 22:32:43
banana apple pear
耗時: 733400納秒 (0.0007334秒)
PS D:\時間操作 chrono 庫\bin\Debug> 

到此這篇關(guān)于C++11計時器:chrono庫介紹的文章就介紹到這了,更多相關(guān)C++ chrono計時器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++的matlab接口轉(zhuǎn)換方法詳解

    C++的matlab接口轉(zhuǎn)換方法詳解

    這篇文章主要為大家詳細(xì)介紹了C++的matlab接口轉(zhuǎn)換方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C++中String類的常用接口函數(shù)總結(jié)

    C++中String類的常用接口函數(shù)總結(jié)

    這篇文章主要介紹了C++中Stirng類的常用接口函數(shù),文中有詳細(xì)的代碼示例供大家參考,對我們學(xué)習(xí)C++有一定的幫助,感興趣的同學(xué)可以跟著小編一起來學(xué)習(xí)
    2023-06-06
  • 零基礎(chǔ)詳解C語言指針進(jìn)階

    零基礎(chǔ)詳解C語言指針進(jìn)階

    在C語言和C++等語言中,數(shù)組元素全為指針變量的數(shù)組稱為指針數(shù)組,指針數(shù)組中的元素都必須具有相同的存儲類型、指向相同數(shù)據(jù)類型的指針變量。指針數(shù)組比較適合用來指向若干個字符串,使字符串處理更加方便、靈活
    2022-02-02
  • 虛函數(shù)表-C++多態(tài)的實(shí)現(xiàn)原理解析

    虛函數(shù)表-C++多態(tài)的實(shí)現(xiàn)原理解析

    這篇文章主要介紹了虛函數(shù)表-C++多態(tài)的實(shí)現(xiàn)原理,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • C++命名空間實(shí)例詳解

    C++命名空間實(shí)例詳解

    這篇文章主要介紹了C++命名空間實(shí)例詳解,有感興趣的同學(xué)可以研究下
    2021-02-02
  • 關(guān)于C/C++中static關(guān)鍵字的作用總結(jié)

    關(guān)于C/C++中static關(guān)鍵字的作用總結(jié)

    以下是對C/C++中static關(guān)鍵字的作用進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下
    2013-09-09
  • C語言動態(tài)內(nèi)存管理的原理及實(shí)現(xiàn)方法

    C語言動態(tài)內(nèi)存管理的原理及實(shí)現(xiàn)方法

    C語言動態(tài)內(nèi)存管理的原理是通過 malloc() 函數(shù)申請一塊連續(xù)的內(nèi)存空間,并返回其地址,通過 free() 函數(shù)釋放該內(nèi)存空間。實(shí)現(xiàn)方法是通過在程序運(yùn)行時動態(tài)地管理內(nèi)存,即在需要內(nèi)存時申請,不需要時釋放,避免了靜態(tài)內(nèi)存分配的浪費(fèi)和不足
    2023-04-04
  • 初識C++的const關(guān)鍵字,常量與常變量

    初識C++的const關(guān)鍵字,常量與常變量

    這篇文章主要為大家詳細(xì)介紹了C++的const關(guān)鍵字,常量與常變量,使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • c++中的自增/自減操作方式

    c++中的自增/自減操作方式

    這篇文章主要介紹了C++中的自增和自減運(yùn)算符,包括前綴和后綴形式,并通過一個具體的例子解釋了自增/自減表達(dá)式的值與函數(shù)參數(shù)傳遞的關(guān)系,文章指出,自增/自減表達(dá)式的值是在表達(dá)式求值時確定的,而不是在自增/自減運(yùn)算后
    2025-03-03
  • C++統(tǒng)計軟件使用時間代碼示例

    C++統(tǒng)計軟件使用時間代碼示例

    這篇文章主要介紹了C++統(tǒng)計軟件使用時間的小程序,大家可以參考使用
    2013-11-11

最新評論

枣强县| 石城县| 垦利县| 海阳市| 乡城县| 故城县| 花莲县| 泸水县| 孝感市| 泸州市| 浦城县| 丰顺县| 谢通门县| 册亨县| 民县| 揭阳市| 葫芦岛市| 古浪县| 西乡县| 泗水县| 涿州市| 安陆市| 香港 | 定陶县| 克山县| 潼关县| 丰城市| 盐津县| 浮山县| 霍邱县| 米易县| 三原县| 秀山| 射阳县| 桃源县| 德化县| 永州市| 长顺县| 永昌县| 乌兰县| 岢岚县|