# time-benchmark **Repository Path**: AELOONG/time-benchmark ## Basic Information - **Project Name**: time-benchmark - **Description**: 各种时间函数基准测试 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-08-19 - **Last Updated**: 2024-08-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 各种时间函数基准测试 ### 函数 #### C/C++ |**函数**|**注解**| |:----|:----| |`std::chrono::system_clock` | |`std::chrono::steady_clock` | |`std::chrono::high_resolution_clock` |**[通常它只是 `std::chrono::steady_clock` 或 `std::chrono::system_clock` 的别名,但具体是哪个取决于库或配置。它是 `system_clock` 时不是单调的(即时间能后退)。例如:如 2023 年所知,libstdc++ 中它是 `system_clock` 的别名“直到高于纳秒的定义变得可行”,MSVC 中它是 `steady_clock` 的别名,而 libc++ 中当 C++ 标准库实现支持单调时钟时为 `steady_clock`,否则为 `system_clock` 的别名。](https://en.cppreference.com/w/cpp/chrono/high_resolution_clock#Notes)**| |`std::chrono::utc_clock` | |`std::chrono::tai_clock` | |`std::chrono::gps_clock` | |`time` | |`clock` | #### Windows
函数 介绍 注解 声明 用法
GetTickCount 检索自系统启动以来经过的毫秒数。 GetTickCount最长为 49.7 天 GetTickCount(64) 函数的分辨率限制为系统计时器的分辨率,通常介于 10 毫秒到 16 毫秒之间。
GetTickCount(64) 函数的分辨率不受 GetSystemTimeAdjustment 函数所做的调整的影响。
DWORD GetTickCount();
GetTickCount64
ULONGLONG GetTickCount64();
GetLocalTime 检索当前本地日期和时间。
void GetLocalTime(
  [out] LPSYSTEMTIME lpSystemTime
);
example
GetSystemTime 检索协调世界时 (UTC) 格式的当前系统日期和时间。
void GetSystemTime(
  [out] LPSYSTEMTIME lpSystemTime
);
GetSystemTimePreciseAsFileTime GetSystemTimePreciseAsFileTime 函数以最高精度 (<1us) 检索当前系统日期和时间。 检索到的信息采用协调世界时 (UTC) 格式。
void GetSystemTimePreciseAsFileTime(
  [out] LPFILETIME lpSystemTimeAsFileTime
);
GetSystemTimes 检索系统计时信息。 在多处理器系统上,返回的值是所有处理器中指定时间的总和。
BOOL GetSystemTimes(
  [out, optional] PFILETIME lpIdleTime,
  [out, optional] PFILETIME lpKernelTime,
  [out, optional] PFILETIME lpUserTime
);
QueryPerformanceCounter 检索性能计数器的当前值,这是一个高分辨率 (<1us) 时间戳,可用于时间间隔度量。
BOOL QueryPerformanceCounter(
  [out] LARGE_INTEGER *lpPerformanceCount
);
example
QueryInterruptTime 获取当前的中断时间计数 timeBeginPeriod 和 timeEndPeriod 函数设置的计时器分辨率会影响 queryInterruptTime 函数的 分辨率。
void QueryInterruptTime(
  [out] PULONGLONG lpInterruptTime
);
example
QueryInterruptTimePrecise 获取当前中断时间计数,其格式比 queryInterruptTime 更精确 。
void QueryInterruptTimePrecise(
  [out] PULONGLONG lpInterruptTimePrecise
);
QueryUnbiasedInterruptTime 获取以 100 纳秒为单位的当前无偏中断时间计数。 无偏见的中断时间计数不包括系统在睡眠或休眠中花费的时间。 timeBeginPeriod 和 timeEndPeriod 函数设置的计时器分辨率会影响 queryInterruptTime 函数的 分辨率。
BOOL QueryUnbiasedInterruptTime(
  PULONGLONG UnbiasedTime
);
QueryUnbiasedInterruptTimePrecise 获取与 queryUnbiasedInterruptTime 相比 ,以更精确的形式获取当前的无偏中断时间计数。 无偏见的中断时间计数不包括系统在睡眠或休眠中花费的时间。
void QueryUnbiasedInterruptTimePrecise(
  [out] PULONGLONG lpUnbiasedInterruptTimePrecise
);
timeGetTime timeGetTime 函数检索系统时间(以毫秒为单位)。 系统时间是 Windows 启动以来经过的时间。 timeGetTime 函数的默认精度可以是 5 毫秒或更多
DWORD timeGetTime();
rdtsc 该指令将返回处理器时间戳。 处理器时间戳记录自上次重置以来的时钟周期数。 在某些版本的 Windows 上、虚拟机的实时迁移以及没有固定或紧密同步 TSC 的硬件系统上,你不会获得可靠的结果。
unsigned __int64 __rdtsc();
example
GetThreadTimes 检索指定线程的计时信息。
BOOL GetThreadTimes(
  [in]  HANDLE     hThread,
  [out] LPFILETIME lpCreationTime,
  [out] LPFILETIME lpExitTime,
  [out] LPFILETIME lpKernelTime,
  [out] LPFILETIME lpUserTime
);
GetProcessTimes 检索指定进程的计时信息。
BOOL GetProcessTimes(
  [in]  HANDLE     hProcess,
  [out] LPFILETIME lpCreationTime,
  [out] LPFILETIME lpExitTime,
  [out] LPFILETIME lpKernelTime,
  [out] LPFILETIME lpUserTime
);
##### Note > [Query[Unbiased]InterruptTime](https://learn.microsoft.com/zh-cn/windows/win32/sysinfo/interrupt-time) 例程链接OneCore.lib 库, 这在MSVC, ClangCL是有效的, 但在MinGW-w64无法找到OneCore.lib。 > > 而链接[QueryInterruptTime[Precise]](https://learn.microsoft.com/zh-cn/windows/win32/api/realtimeapiset/nf-realtimeapiset-queryinterrupttime#requirements)等中断函数的介绍页的要求的Mincore.lib是可以正常使用的。 > > [QueryUnbiasedInterruptTime[Precise]](https://learn.microsoft.com/zh-cn/windows/win32/api/realtimeapiset/nf-realtimeapiset-queryinterrupttimeprecise)虽然没有要求额外的库,但实践中仍然需要链接OneCore.lib/Mincore.lib。