日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > > 程序喵大人
[導(dǎo)讀]大家平時工作,如果有計算函數(shù)耗時或者打印當(dāng)前時間的需求,一定要來看看這篇文章!

大家平時工作,如果有計算函數(shù)耗時或者打印當(dāng)前時間的需求,一定要來看看這篇文章!

首先介紹下C++標(biāo)準(zhǔn)中的chrono

chrono是一個關(guān)于時間的庫,起源于boost,現(xiàn)在是C++的標(biāo)準(zhǔn),話說現(xiàn)在的C++標(biāo)準(zhǔn)好多都是源于boost,要進(jìn)標(biāo)準(zhǔn)的特性似乎都會先在boost試驗(yàn)一番。

首先看一下使用chrono簡單計時的示例代碼:

void func() { // 計時 std::chrono::time_point<std::chrono::high_resolution_clock> begin = high_resolution_clock::now(); std::this_thread::sleep_for(std::chrono::milliseconds(20)); auto end = high_resolution_clock::now(); cout << "time " << duration_cast(end - begin).count() << endl;}

chrono中有三個概念duration、time_point、clock

duration:表示一段時間,三分鐘、三秒等,它的定義如下:

template <class _Rep, class _Period = ratio<1>> class duration;

ratio的定義如下:

 template <intmax_t N, intmax_t D = 1> class ratio;

Rep表示數(shù)據(jù)類型,int,long等,Period表示時間單位,N是分子,D是分母,直接看例子吧:

using atto = ratio<1, 1000000000000000000LL>;using femto = ratio<1, 1000000000000000LL>;using pico = ratio<1, 1000000000000LL>;using nano = ratio<1, 1000000000>;using micro = ratio<1, 1000000>;using milli = ratio<1, 1000>;using centi = ratio<1, 100>;using deci = ratio<1, 10>;using deca = ratio<10, 1>;using hecto = ratio<100, 1>;using kilo = ratio<1000, 1>;using mega = ratio<1000000, 1>;using giga = ratio<1000000000, 1>;using tera = ratio<1000000000000LL, 1>;using peta = ratio<1000000000000000LL, 1>;using exa = ratio<1000000000000000000LL, 1>;
using nanoseconds = duration<long long, nano>;using microseconds = duration<long long, micro>;using milliseconds = duration<long long, milli>;using seconds = duration<long long>;using minutes = duration<int, ratio<60>>;using hours = duration<int, ratio<3600>>;
using hours2 = duration<int, ratio<3600, 1>>;using hours2 = duration<int, ratio<7200, 2>>;

詳細(xì)看完上述例子您也明白了,ratio的默認(rèn)的時間單位是1秒,以小時為例,一小時等于3600秒,3600 / 1 == 7200 / 2 == 3600,所以hours == hours2 == hours3。

標(biāo)準(zhǔn)庫還提供了duration_cast用于轉(zhuǎn)換各種duration。

template <class _To, class _Rep, class _Period, enable_if_t<_Is_duration_v<_To>, int> = 0> constexpr _To duration_cast(const duration<_Rep, _Period>&) noexcept( is_arithmetic_v<_Rep>&& is_arithmetic_v);
template <class _Ty> _INLINE_VAR constexpr bool _Is_duration_v = _Is_specialization_v<_Ty, duration>;
template <class _Ty>_INLINE_VAR constexpr bool is_arithmetic_v = // determine whether _Ty is an arithmetic type is_integral_v<_Ty> || is_floating_point_v<_Ty>;

函數(shù)看著很繁瑣,直接看看示例代碼吧:

void func() { auto sec = std::chrono::seconds(10); auto mill = std::chrono::duration_cast<std::chrono::milliseconds>(sec); cout << sec.count() << endl; // 返回多少s cout << mill.count() << endl; // 返回多少ms}輸出:1010000

time_point:用來表示某個具體時間點(diǎn)。

定義如下:

template <class _Clock, class _Duration = typename _Clock::duration> class time_point;

使用方式如下:

void func() { std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp(std::chrono::seconds(12)); cout << tp.time_since_epoch().count() << endl; std::time_t t = system_clock::to_time_t(tp); cout << "time " << ctime(&t) << endl;}輸出:12000time Thu Jan 1 08:00:12 1970

這里有個函數(shù)time_since_epoch(),表示這個time_point距離元年也就是1970年1月1日所經(jīng)過的duration。

time_point也有各種表示方式,類似于duration,也提供了轉(zhuǎn)換函數(shù)time_point_cast()。

void func() { time_point tp(seconds(12)); cout << tp.time_since_epoch().count() << endl; time_point tp2 = time_point_cast(tp); cout << tp2.time_since_epoch().count() << endl;}輸出:1200012

Clocks

這里的時鐘大體有三種:

system_clock

steady_clock

high_resolution_clock

system_clock表示當(dāng)前的系統(tǒng)時鐘,有三個函數(shù):

now():表示當(dāng)前時間的time_pointto_time_t():將time_point轉(zhuǎn)換成time_t秒from_time_t():將time_t轉(zhuǎn)換成time_point

源碼如下:

struct system_clock { // wraps GetSystemTimePreciseAsFileTime/GetSystemTimeAsFileTime using rep = long long;
using period = ratio_multiply1>, nano>;
using duration = chrono::duration; using time_point = chrono::time_point; static constexpr bool is_steady = false;
_NODISCARD static time_point now() noexcept { // get current time return time_point(duration(_Xtime_get_ticks())); }
_NODISCARD static __time64_t to_time_t(const time_point& _Time) noexcept { // convert to __time64_t return static_cast<__time64_t>(_Time.time_since_epoch().count() / _XTIME_TICKS_PER_TIME_T); }
_NODISCARD static time_point from_time_t(__time64_t _Tm) noexcept { // convert from __time64_t return time_point(duration(_Tm * _XTIME_TICKS_PER_TIME_T)); }};

steady_clock表示穩(wěn)定的時鐘,它只有一個函數(shù),就是now(),后一次調(diào)用now()肯定比上一次調(diào)用now()的返回值大,不受系統(tǒng)時間修改的影響。

源碼如下:

struct steady_clock { // wraps QueryPerformanceCounter using rep = long long; using period = nano; using duration = nanoseconds; using time_point = chrono::time_point; 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."); const long long _Whole = (_Ctr / _Freq) * period::den; const long long _Part = (_Ctr % _Freq) * period::den / _Freq; return time_point(duration(_Whole + _Part)); }};

使用方式和之前的都相同:

void func() { // 計時 std::chrono::time_point<std::chrono::steady_clock> begin = steady_clock::now(); std::this_thread::sleep_for(std::chrono::milliseconds(20)); auto end = steady_clock::now(); cout << "time " << duration_cast(end - begin).count() << endl;}

high_resolution_clock表示高精度時鐘,是系統(tǒng)可用的最高精度的時鐘,它其實(shí)就是system_clock或者steady_clock的別名:

using high_resolution_clock = steady_clock;

介紹完了C++的chrono那下面再看下C語言的各種時間相關(guān)的API吧:

首先可以通過C語言的clock拿到程序執(zhí)行時處理器所使用的時鐘數(shù)來計時:

clock_t clock(void);

該函數(shù)返回程序執(zhí)行起(一般為程序的開頭),處理器時鐘所使用的時間。也獲取 CPU 所使用的秒數(shù),除以 CLOCKS_PER_SEC即可,返回的clock_t其實(shí)就是long類型的重命名。

使用方式如下:

void func() { clock_t start_t = clock(); cout << start_t << " 個時鐘 \n"; for (int i = 0; i < 100000000; i++) { } clock_t end_t = clock(); cout << end_t << " 個時鐘 \n"; cout << "循環(huán)的秒數(shù):" << (double)(end_t - start_t) / CLOCKS_PER_SEC << endl;}

如何獲取當(dāng)前時間戳,單位為秒

void func() { // 獲取當(dāng)前時間戳,單位為秒 struct timeval time; gettimeofday(&time, NULL); cout << time.tv_sec << " s \n";}

也可以使用time函數(shù):

time_t time(time_t *time);

該函數(shù)返回系統(tǒng)的當(dāng)前日歷時間,返回的是自1970年1月1日以來所經(jīng)過的秒數(shù)。

time_t其實(shí)就是一個整數(shù)類型,是int64_t的重命名,該函數(shù)直接使用返回值就好,參數(shù)一般傳空即可。

timer 存取結(jié)果的時間指針變量,類型為time_t,指針變量可以為null。

如果timer指針非null,則time()函數(shù)返回值變量與timer指針一樣,都指向同一個內(nèi)存地址;

否則如果timer指針為null,則time()函數(shù)返回一個time_t變量時間。

void func() { // 獲取當(dāng)前時間戳,單位為秒 time_t now = time(NULL); cout << static_cast<int64_t>(now) << " s \n";}

如何獲取當(dāng)前時間戳?單位為毫秒

void func() { // 獲取當(dāng)前時間戳,單位為毫秒 struct timeval time; gettimeofday(&time, NULL); cout << time.tv_sec * 1000 + time.tv_usec / 1000 << " ms \n";}

如何顯示當(dāng)前的系統(tǒng)時間呢?可以使用ctime顯示當(dāng)前時間:

char* ctime(const time_t* time);

該函數(shù)返回一個表示當(dāng)?shù)貢r間的字符串指針,輸出內(nèi)容格式如下:

day month year hours:minutes:seconds year\n\0。

示例代碼如下:

void func() { time_t now = time(NULL); char* dt = ctime(&now); cout << "cur time is: " << dt;}輸出:Tue Sep 22 22:01:40 2020

可以使用tm結(jié)構(gòu)自定義顯示當(dāng)前時間的格式:

struct tm * localtime(const time_t * timer);

將日歷時間轉(zhuǎn)換為本地時間,從1970年起始的時間戳轉(zhuǎn)換為1900年起始的時間數(shù)據(jù)結(jié)構(gòu)

另一個類似的函數(shù)是gmtime函數(shù):

struct tm *gmtime(const time_t *time);

只是該函數(shù)返回的是UTC時間,協(xié)調(diào)世界時(UTC)也被稱為格林尼治標(biāo)準(zhǔn)時間(GMT)。

tm結(jié)構(gòu)如下:

struct tm { int tm_sec; // 秒,正常范圍從 0 到 59,但允許至 61 int tm_min; // 分,范圍從 0 到 59 int tm_hour; // 小時,范圍從 0 到 23 int tm_mday; // 一月中的第幾天,范圍從 1 到 31 int tm_mon; // 月,范圍從 0 到 11 int tm_year; // 自 1900 年起的年數(shù) int tm_wday; // 一周中的第幾天,范圍從 0 到 6,從星期日算起 int tm_yday; // 一年中的第幾天,范圍從 0 到 365,從 1 月 1 日算起 int tm_isdst; // 夏令時};

tm_sec 在C89的范圍是[0-61],在C99更正為[0-60]。通常范圍是[0-59],貌似有些系統(tǒng)會出現(xiàn)60秒的跳躍。

tm_mon 是從零開始的,所以一月份為0,十二月份為11。

tm_year是從1900年開始計算,所以顯示年份的時候需要加上1900

void func() { time_t rawtime = time(NULL); struct tm* ptminfo = localtime(&rawtime); printf("cur time is: %02d-%02d-%02d %02d:%02d:%02d\n", ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday, ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec); ptminfo = gmtime(&rawtime); printf("cur time is: %02d-%02d-%02d %02d:%02d:%02d\n", ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday, ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);}輸出:cur time is: 2020-09-23 21:27:37cur time is: 2020-09-23 13:27:37

可以通過asctime顯示tm結(jié)構(gòu)的時間:

char * asctime ( const struct tm * time );

和ctime類似,返回的都是一個固定時間格式的字符串,只是傳入的參數(shù)不同。

void func() { time_t rawtime = time(NULL); struct tm* info1 = localtime(&rawtime); cout << "正常 日期和時間:" << asctime(info1) << endl; info1 = gmtime(&rawtime); cout << "UTC 日期和時間:" << asctime(info1) << endl;}輸出:正常 日期和時間:Wed Sep 23 21:47:44 2020UTC 日期和時間:Wed Sep 23 13:47:44 2020

也可以使用strftime()函數(shù),該函數(shù)可用于格式化日期和時間為指定的格式,如果產(chǎn)生的 C 字符串小于 size 個字符(包括空結(jié)束字符),則會返回復(fù)制到 str 中的字符總數(shù)(不包括空結(jié)束字符),否則返回零。

size_t strftime( char *str, // 指向目標(biāo)數(shù)組的指針,用來復(fù)制產(chǎn)生的C字符串 size_t maxsize, // 最多傳出字符數(shù)量 const char *format, // 格式化方式 const struct tm *timeptr // tm指針);

format格式如下:

%a 星期幾的縮寫%A 星期幾的全稱%b 月份的縮寫%B 月份的全稱%c 標(biāo)準(zhǔn)的日期的時間串%C 年份的前兩位數(shù)字%d 十進(jìn)制表示的每月的第幾天(值從1到31)%D 月/天/年%e 在兩字符域中,十進(jìn)制表示的每月的第幾天%F 年-月-日%g 年份的后兩位數(shù)字,使用基于周的年%G 年份,使用基于周的年%h 簡寫的月份名%H 24小時制的小時(值從0到23)%I 12小時制的小時(值從1到12)%j 十進(jìn)制表示的每年的第幾天(值從1到366)%m 十進(jìn)制表示的月份(值從1到12)%M 十時制表示的分鐘數(shù)(值從0到59)%n 換行符%p 本地的AM或PM的等價顯示%r 12小時的時間%R 顯示小時和分鐘:hh:mm%S 十進(jìn)制的秒數(shù)(值從0到61)%t 水平制表符%T 顯示時分秒:hh:mm:ss%u 每周的第幾天,星期一為第一天 (值從1到7,星期一為1)%U 第年的第幾周,把星期日作為第一天(值從0到53)%V 每年的第幾周,使用基于周的年%w 十進(jìn)制表示的星期幾(值從0到6,星期天為0)%W 每年的第幾周,把星期一做為第一天(值從0到53)%x 標(biāo)準(zhǔn)的日期串%X 標(biāo)準(zhǔn)的時間串%y 不帶世紀(jì)的十進(jìn)制年份(值從0到99)%Y 帶世紀(jì)部分的十制年份%Z 時區(qū)名稱,如果不能得到時區(qū)名稱則返回空字符。%% 一個%符號

使用代碼如下:

void func() { time_t rawtime = time(NULL); char buf[256]; strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&rawtime)); cout << buf << endl;}


參考資料:

https://www.runoob.com/cprogramming/c-function-strftime.html

https://www.runoob.com/cprogramming/c-function-clock.html

https://www.runoob.com/cplusplus/cpp-date-time.html

https://www.cnblogs.com/jwk000/p/3560086.html




往期推薦





免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點(diǎn),不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀

全球嵌入式技術(shù)領(lǐng)域的年度盛會2026嵌入式世界展(Embedded World 2026,簡稱EW26)于3月10日至12日在德國紐倫堡成功舉辦。作為物聯(lián)網(wǎng)和邊緣AI領(lǐng)域的領(lǐng)先企業(yè),Silicon Labs(亦稱“芯科科...

關(guān)鍵字: 物聯(lián)網(wǎng) 邊緣AI 嵌入式

3月10日至12日,2026年嵌入式世界展(Embedded World 2026,簡稱EW26)在德國紐倫堡展覽中心成功舉辦。作為領(lǐng)先的邊緣AI與智能音頻等媒體處理技術(shù)和芯片解決方案提供商,XMOS以沉浸式演示與技術(shù)交...

關(guān)鍵字: 邊緣AI 智能音頻 嵌入式

在嵌入式系統(tǒng)開發(fā)中,SoC(System on Chip)的多樣性始終是橫亙在開發(fā)者面前的難題。以某工業(yè)物聯(lián)網(wǎng)網(wǎng)關(guān)項目為例,其需同時支持NXP i.MX8M、Rockchip RK3566和Allwinner H616三...

關(guān)鍵字: Platform Driver模型 嵌入式

在高性能網(wǎng)絡(luò)編程領(lǐng)域,事件驅(qū)動模型以其高效的I/O多路復(fù)用能力成為主流范式。不同于傳統(tǒng)的多線程/多進(jìn)程阻塞模型,事件驅(qū)動通過單一線程監(jiān)聽多個文件描述符的狀態(tài)變化,以非阻塞方式處理I/O事件,顯著減少了上下文切換開銷和資源...

關(guān)鍵字: 事件驅(qū)動 C語言

在非易失性存儲器領(lǐng)域,EEPROM(電可擦除可編程只讀存儲器)曾長期占據(jù)主流地位,廣泛應(yīng)用于各類電子設(shè)備的參數(shù)存儲、日志記錄等場景。但隨著工業(yè)控制、汽車電子、醫(yī)療設(shè)備等領(lǐng)域?qū)Υ鎯π阅芴岢龈咭螅現(xiàn)RAM(鐵電隨機(jī)存取存...

關(guān)鍵字: 存儲器 可編程 嵌入式

康佳特將aReady.COM擴(kuò)展至Arm架構(gòu)模塊,基于恩智浦i.MX 95處理器打造應(yīng)用就緒的軟硬件構(gòu)建模塊,集成操作系統(tǒng)、系統(tǒng)整合與IoT連接能力,賦能高價值應(yīng)用快速落地

關(guān)鍵字: 處理器 IoT 嵌入式

3月12日,2026年中國家電及消費(fèi)電子博覽會(以下簡稱:AWE 2026)在上海盛大開幕。展會現(xiàn)場,場景化、系統(tǒng)化、一體化的家電解決方案成為行業(yè)焦點(diǎn),消費(fèi)者對家電的關(guān)注也已從基礎(chǔ)的尺寸匹配,延伸至對“空間秩序感”和“視...

關(guān)鍵字: 消費(fèi)電子 蒸烤箱 嵌入式

超高效NPU IP在資源受限設(shè)備中推進(jìn)邊緣AI,因而獲得認(rèn)可

關(guān)鍵字: 人工智能 嵌入式 NPU

上海2026年3月12日 /美通社/ -- 3月12日,2026中國家電及消費(fèi)電子博覽會(AWE 2026)在上海新國際博覽中心和東方樞紐國際商務(wù)合作區(qū)展區(qū)正式啟幕。本屆展會以"AI科技?慧享未來"為...

關(guān)鍵字: 西門子 博世 嵌入式 洗碗機(jī)
關(guān)閉