C++實現(xiàn)獲取系統(tǒng)時間的方法小結(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ù)獲取的時間是以微秒為單位的。因此,需要將微秒轉(zhuǎn)換為秒,即除以 1e6,以獲得以秒為單位的時間間隔。
方法二
使用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ù)獲取的時間是以納秒為單位的。因此,需要將納秒轉(zhuǎn)換為秒,即除以 1e9,以獲得以秒為單位的時間間隔。
Windows
方法一
在Windows平臺上,可以使用Windows API中的GetSystemTime()、GetSystemTimeAsFileTime()或QueryPerformanceCounter()等函數(shù)來獲取系統(tǒng)時間。下面是一個簡單的示例代碼,
#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>頭文件,提供了高精度的時間測量功能??梢允褂胹td::chrono::high_resolution_clock來獲取高分辨率時鐘,并通過std::chrono::time_point計算時間間隔。以下是一個示例代碼,
#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)庫中的<ctime>頭文件提供了clock()函數(shù),可用于測量CPU時間。這個函數(shù)返回自程序啟動以來的時鐘周期數(shù)。以下是一個簡單示例,
#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++實現(xiàn)獲取系統(tǒng)時間的方法小結(jié)的文章就介紹到這了,更多相關(guān)C++獲取系統(tǒng)時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT連接Mysql數(shù)據(jù)庫的實現(xiàn)步驟
本文主要介紹了QT連接Mysql數(shù)據(jù)庫的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C++類重載函數(shù)的function和bind使用示例
這篇文章主要介紹了C++類重載函數(shù)的function和bind使用示例,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下2021-01-01C++中函數(shù)模板與類模板的簡單使用及區(qū)別介紹
這篇文章介紹了C++中的模板機(jī)制,包括函數(shù)模板和類模板的概念、語法和實際應(yīng)用,函數(shù)模板通過類型參數(shù)實現(xiàn)泛型操作,而類模板允許創(chuàng)建可處理多種數(shù)據(jù)類型的類,文章還討論了模板的關(guān)鍵區(qū)別、注意事項以及它們在實際編程中的應(yīng)用,感興趣的朋友一起看看吧2025-03-03