亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++11計(jì)時(shí)器之chrono庫簡(jiǎn)介

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

C++11計(jì)時(shí)器:chrono庫介紹

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

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

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

一 、時(shí)鐘-CLOCK

chrono庫定義了三種不同的時(shí)鐘:

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

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

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

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

high_resolution_clock則是一個(gè)高分辨率時(shí)鐘。

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

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

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

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

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

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

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

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

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

#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<<"程序用時(shí)="<<tt.count()<<"微秒"<<endl;
    return 0;
}

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

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

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

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

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

另一個(gè)實(shí)例:

#define _CRT_SECURE_NO_WARNINGS //localtime()需要這個(gè)宏
#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)();
    //     }
    // };
//時(shí)間長(zhǎng)度
void Func1(){
    //chrono重載了各種運(yùn)算符
    std::chrono::hours             c1(1);                //1小時(shí)
    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;
    }
    //獲取時(shí)鐘周期的值,返回的是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)時(shí)間
/**
 * @brief 
 * 
 * system_clock 類支持了對(duì)系統(tǒng)時(shí)鐘的訪問, 提供了三個(gè)靜態(tài)成員函數(shù):
返回當(dāng)前時(shí)間的時(shí)間點(diǎn)。
static std::chrono::time_point<std::chrono::system_clock> now() noexcept;
將時(shí)間點(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)換為時(shí)間點(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
    //這些都可以簡(jiǎn)寫用auto now=std::chrono::system_clock()::now()接收
    //1、靜態(tài)成員函數(shù) static std::chrono::system_clock::now()用來獲取系統(tǒng)時(shí)間,C++時(shí)間
    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)時(shí)間轉(zhuǎn)換為time_t (utc時(shí)間)
    time_t t_now=std::chrono::system_clock::to_time_t(now);
    //3、std::localtime()函數(shù)把time_t時(shí)間轉(zhuǎn)換為本地時(shí)間(北京時(shí)間)
    // 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++一般不打印出時(shí)間,而是把時(shí)間存儲(chǔ)到一個(gè)字符串中
    std::stringstream ss;  //創(chuàng)建stringstream對(duì)象 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
//計(jì)時(shí)器 steady_clock 類相當(dāng)于秒表,操作系統(tǒng)只要啟動(dòng)就會(huì)進(jìn)行時(shí)間的累加,常用于耗時(shí)的統(tǒng)計(jì)(精確到納秒) 。
void Func3(){
    //靜態(tài)成員函數(shù)std::chrono::steady_clock::now()獲取時(shí)間的開始點(diǎn)
    std::chrono::time_point<std::chrono::steady_clock> start=std::chrono::steady_clock::now();
    //auto start=std::chrono::steady_clock::now();
    //執(zhí)行一些代碼,消耗時(shí)間
    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()獲取時(shí)間的結(jié)束點(diǎn)
    auto end=std::chrono::steady_clock::now();
    //計(jì)算消耗的時(shí)間,單位是納秒
    auto dt=end-start;
    std::cout<<"耗時(shí): "<<dt.count()<<"納秒 ("<<(double)dt.count()/(1000*1000*1000)<<"秒) "<<std::endl;
}
int main(int argc,char* argv[]){
    Func1();
    Func2();
    Func3();
    return 0;
}

輸出結(jié)果:

PS D:\時(shí)間操作 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
耗時(shí): 733400納秒 (0.0007334秒)
PS D:\時(shí)間操作 chrono 庫\bin\Debug> 

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

相關(guān)文章

  • C語言實(shí)現(xiàn)超市信息管理系統(tǒng)

    C語言實(shí)現(xiàn)超市信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)超市信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++實(shí)現(xiàn)選擇排序(selectionSort)

    C++實(shí)現(xiàn)選擇排序(selectionSort)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)選擇排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 構(gòu)造函數(shù)不能聲明為虛函數(shù)的原因及分析

    構(gòu)造函數(shù)不能聲明為虛函數(shù)的原因及分析

    構(gòu)造函數(shù)不需要是虛函數(shù),也不允許是虛函數(shù),因?yàn)閯?chuàng)建一個(gè)對(duì)象時(shí)我們總是要明確指定對(duì)象的類型,盡管我們可能通過實(shí)驗(yàn)室的基類的指針或引用去訪問它但析構(gòu)卻不一定,我們往往通過基類的指針來銷毀對(duì)象
    2013-10-10
  • C語言 指針綜合解析

    C語言 指針綜合解析

    指針可以表示一個(gè)變更的地址,在計(jì)算機(jī)程序中,通常表示內(nèi)存地址,存儲(chǔ)數(shù)據(jù)的地址,下面這篇文章主要給大家綜合的介紹了關(guān)于C語言指針的本質(zhì)與用法
    2021-11-11
  • C語言學(xué)習(xí)之條件和?if...else語句詳解

    C語言學(xué)習(xí)之條件和?if...else語句詳解

    這篇文章主要給大家介紹了C語言中的條件和?if...else語句,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-12-12
  • C語言實(shí)現(xiàn)可保存的動(dòng)態(tài)通訊錄的示例代碼

    C語言實(shí)現(xiàn)可保存的動(dòng)態(tài)通訊錄的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用C語言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的可保存的動(dòng)態(tài)通訊錄,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下
    2022-07-07
  • Qt實(shí)現(xiàn)定時(shí)器的兩種方法分享

    Qt實(shí)現(xiàn)定時(shí)器的兩種方法分享

    這篇文章主要為大家詳細(xì)介紹了Qt中實(shí)現(xiàn)定時(shí)器的兩種不同方法,文中的示例代碼講解詳細(xì),對(duì)我們了解Qt有一定的幫助,感興趣的可以跟隨小編一起學(xué)習(xí)一下
    2022-11-11
  • 最新評(píng)論