C++中windows&linux平台的时间函数

c++比较时间大小函数

Posted by CS on July 31, 2024

C++中windows&linux平台的时间函数

在 C++ 编程中,时间函数的选择对于性能测量、任务调度和时间戳记录至关重要。不同的操作系统提供了不同的时间函数,同时在同一个平台上,也可能有多种不同的时间函数可供选择。本文将介绍在 C++ 中常用的时间函数,并比较它们在不同平台上的应用和效果。

跨平台的时间函数:std::chrono

随着 C++11 的引入,标准库提供了 std::chrono,这是一个现代化的时间库,具有高精度和跨平台的特性。它基于类型安全和模板化的设计,使得时间的测量和计算变得更加简单和可靠。 在这个示例中,std::chrono::high_resolution_clock 提供了高分辨率的时间点,std::chrono::duration<double> 用于表示时间间隔。这些功能在大多数现代操作系统上都可用,因此非常适合跨平台开发。

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <chrono>
#include <thread> 
int main() 
{    
    auto start = std::chrono::high_resolution_clock::now(); // 开始时间戳   
    std::this_thread::sleep_for(std::chrono::seconds(1));   // 延时1秒   
    auto end = std::chrono::high_resolution_clock::now();   // 结束时间戳
    // 可选单位:秒(默认),毫秒(milli),微秒(micro),纳秒(nano)      
    std::chrono::duration<double, std::milli> duration = end - start;    
    std::cout << "Duration: " << duration.count() << " seconds\n";     
    return 0;
}

Windows 平台的时间函数

1. QueryPerformanceCounter

Windows 上的 GetSystemTime 提供了系统时间,而 QueryPerformanceCounter 则提供了高精度的计时器功能。

2. GetSystemTime 和 GetLocalTime

这些函数提供了系统时间和本地时间的访问(不太适合计时):

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <windows.h>
int main() 
{    
    SYSTEMTIME st;    
    GetSystemTime(&st); // 获取系统时间(UTC 时间)     
    std::cout << "System Time (UTC): " << st.wYear << "-" << st.wMonth << "-" << st.wDay << " " << st.wHour << ":" << st.wMinute << ":" << st.wSecond << "." << st.wMilliseconds << "\n";     
    GetLocalTime(&st); // 获取本地时间     
    std::cout << "Local Time: " << st.wYear << "-" << st.wMonth << "-" << st.wDay << " " << st.wHour << ":" << st.wMinute << ":" << st.wSecond << "." << st.wMilliseconds << "\n";     
}

这是一个高精度的计时器,适合精确测量时间间隔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <windows.h> 
int main() 
{    
    LARGE_INTEGER frequency;    
    LARGE_INTEGER start, end;              // 获取高精度计时器的频率    
    QueryPerformanceFrequency(&frequency); // 获取开始时间    
    QueryPerformanceCounter(&start);       // 模拟工作(例如,暂停 1 秒)    
    Sleep(1000);                           // 获取结束时间    
    QueryPerformanceCounter(&end);         // 计算持续时间    
    double duration = static_cast<double>(end.QuadPart - start.QuadPart) / frequency.QuadPart;    
    std::cout << "High-resolution duration: " << duration << " seconds\n";     
    return 0;
}

Unix/Linux 平台的时间函数

在 Unix/Linux 系统上,也有多种时间函数可供选择。 在 Unix/Linux 上,gettimeofdayclock_gettime 分别提供了不同精度和用途的时间测量。

1. gettimeofday

这是一个高分辨率的计时函数,返回自 Epoch 以来的秒数和微秒数:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <sys/time.h> 
int main() 
{
    struct timeval tv;    
    gettimeofday(&tv, nullptr);     
    std::cout << "Seconds: " << tv.tv_sec << "\n";    
    std::cout << "Microseconds: " << tv.tv_usec << "\n";     
    return 0;
}

2. clock_gettime

提供了更高的精度,并支持多种时间类型:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <ctime> 
int main() 
{    
    struct timespec ts;    
    clock_gettime(CLOCK_MONOTONIC, &ts);     
    std::cout << "Seconds: " << ts.tv_sec << "\n";    
    std::cout << "Nanoseconds: " << ts.tv_nsec << "\n";     
    return 0;
}

使用跨平台库

除了原生的操作系统时间函数外,还可以考虑使用跨平台的第三方库,如 Boost库中的时间模块。Boost.Chrono 提供了与 std::chrono 类似的功能,同时保持了更好的兼容性和可移植性。

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <boost/chrono.hpp> 
int main() 
{    
    boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();     // 模拟工作    
    boost::this_thread::sleep_for(boost::chrono::seconds(1));     
    boost::chrono::high_resolution_clock::time_point end = boost::chrono::high_resolution_clock::now();    
    boost::chrono::duration<double> duration = end - start;     
    std::cout << "Duration: " << duration.count() << " seconds\n";    
    return 0;
}

结论

选择合适的时间函数取决于你的应用程序需求,如精度、平台兼容性和功能特性。在现代 C++ 中,std::chrono 提供了一个强大的跨平台时间库,推荐用于大多数时间测量和计时任务。而对于特定平台或需要更高精度的情况,可以考虑使用操作系统提供的特定时间函数或第三方库进行扩展。