C++實(shí)現(xiàn)獲取系統(tǒng)時(shí)間的方法小結(jié)
Linux
方法一
使用time.h中的gettimeofday(),示例代碼如下,
#include <iostream> #include <sys/time.h> int main() { struct timeval start, end; double totalTime; gettimeofday(&start, NULL); // 在這里執(zhí)行代碼 ... gettimeofday(&end, NULL); totalTime = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0; std::cout << "Total time: " << totalTime << " seconds" << std::endl; return 0; }
使用gettimeofday()函數(shù)獲取的時(shí)間是以微秒為單位的。因此,需要將微秒轉(zhuǎn)換為秒,即除以 1e6,以獲得以秒為單位的時(shí)間間隔。
方法二
使用time.h中的clock_gettime(),示例代碼如下,
#include <iostream> #include <time.h> int main() { struct timespec start, end; double totalTime; clock_gettime(CLOCK_MONOTONIC, &start); // 在這里執(zhí)行代碼 ... clock_gettime(CLOCK_MONOTONIC, &end); totalTime = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; std::cout << "Total time: " << totalTime << " seconds" << std::endl; return 0; }
使用clock_gettime()函數(shù)獲取的時(shí)間是以納秒為單位的。因此,需要將納秒轉(zhuǎn)換為秒,即除以 1e9,以獲得以秒為單位的時(shí)間間隔。
Windows
方法一
在Windows平臺(tái)上,可以使用Windows API中的GetSystemTime()、GetSystemTimeAsFileTime()或QueryPerformanceCounter()等函數(shù)來(lái)獲取系統(tǒng)時(shí)間。下面是一個(gè)簡(jiǎn)單的示例代碼,
#include <iostream> #include <windows.h> int main() { LARGE_INTEGER frequency, start, end; double totalTime; QueryPerformanceFrequency(&frequency); QueryPerformanceCounter(&start); // 在這里執(zhí)行代碼 ... QueryPerformanceCounter(&end); totalTime = static_cast<double>(end.QuadPart - start.QuadPart) / frequency.QuadPart; std::cout << "Total time: " << totalTime << " seconds" << std::endl; return 0; }
方法二
C++11引入了<chrono>頭文件,提供了高精度的時(shí)間測(cè)量功能。可以使用std::chrono::high_resolution_clock來(lái)獲取高分辨率時(shí)鐘,并通過(guò)std::chrono::time_point計(jì)算時(shí)間間隔。以下是一個(gè)示例代碼,
#include <iostream> #include <chrono> int main() { auto start = std::chrono::high_resolution_clock::now(); // 在這里執(zhí)行代碼 ... auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> totalTime = end - start; std::cout << "Total time: " << totalTime.count() << " seconds" << std::endl; return 0; }
方法三
C++標(biāo)準(zhǔn)庫(kù)中的<ctime>頭文件提供了clock()函數(shù),可用于測(cè)量CPU時(shí)間。這個(gè)函數(shù)返回自程序啟動(dòng)以來(lái)的時(shí)鐘周期數(shù)。以下是一個(gè)簡(jiǎn)單示例,
#include <iostream> #include <ctime> int main() { clock_t start = clock(); // 在這里執(zhí)行代碼 ... clock_t end = clock(); double totalTime = static_cast<double>(end - start) / CLOCKS_PER_SEC; std::cout << "Total time: " << totalTime << " seconds" << std::endl; return 0; }
到此這篇關(guān)于C++實(shí)現(xiàn)獲取系統(tǒng)時(shí)間的方法小結(jié)的文章就介紹到這了,更多相關(guān)C++獲取系統(tǒng)時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT連接Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟
本文主要介紹了QT連接Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C++類(lèi)重載函數(shù)的function和bind使用示例
這篇文章主要介紹了C++類(lèi)重載函數(shù)的function和bind使用示例,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下2021-01-01C語(yǔ)言實(shí)現(xiàn)多項(xiàng)式的相加
這篇文章主要為大家介紹了C語(yǔ)言實(shí)現(xiàn)多項(xiàng)式的相加,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Windows程序內(nèi)部運(yùn)行機(jī)制實(shí)例詳解
這篇文章主要介紹了Windows程序內(nèi)部運(yùn)行機(jī)制實(shí)例詳解,對(duì)于學(xué)習(xí)Windows程序設(shè)計(jì)來(lái)說(shuō)是非常重要的一課,需要的朋友可以參考下2014-08-08C++中函數(shù)模板與類(lèi)模板的簡(jiǎn)單使用及區(qū)別介紹
這篇文章介紹了C++中的模板機(jī)制,包括函數(shù)模板和類(lèi)模板的概念、語(yǔ)法和實(shí)際應(yīng)用,函數(shù)模板通過(guò)類(lèi)型參數(shù)實(shí)現(xiàn)泛型操作,而類(lèi)模板允許創(chuàng)建可處理多種數(shù)據(jù)類(lèi)型的類(lèi),文章還討論了模板的關(guān)鍵區(qū)別、注意事項(xiàng)以及它們?cè)趯?shí)際編程中的應(yīng)用,感興趣的朋友一起看看吧2025-03-03