问题描述
C/C++ 程序员可以使用的最快计时系统是什么?
例如:
time() 将给出自 1970 年 1 月 1 日 00:00 以来的秒数.
Windows 上的 GetTickCount() 将给出自系统启动以来的时间(以毫秒为单位),但限制为 49.7 天(之后它会简单地回零).
我想以毫秒为单位获取当前时间或自系统/应用程序启动以来的滴答声.
最大的问题是方法的开销 - 我需要最轻的一个,因为我将要每秒调用它很多次.
我的情况是我有一个工作线程,并且我向该工作线程发布了待处理的工作.每个作业都有一个执行时间".所以,我不在乎时间是当前的真实"时间还是自系统正常运行以来的时间 - 它必须是线性的和轻量级的.
unsigned __int64 GetTickCountEx(){静态 DWORD dwWraps = 0;静态 DWORD dwLast = 0;DWORD dwCurrent = 0;timeMutex.lock();dwCurrent = GetTickCount();如果(dwLast > dwCurrent)dwWraps++;dwLast = dwCurrent;无符号 __int64 时间结果 = ((无符号 __int64)0xFFFFFFFF * dwWraps) + dwCurrent;timeMutex.unlock();返回时间结果;}
为了计时,当前微软推荐是使用QueryPerformanceCounter
&QueryPerformanceFrequency
.
这将为您提供优于毫秒的计时.如果系统不支持高分辨率计时器,则默认为毫秒(与 GetTickCount
相同).
这是一篇简短的 Microsoft 文章,其中包含您应该使用它的原因的示例 :)>
What is the fastest timing system a C/C++ programmer can use?
For example:
time() will give the seconds since Jan 01 1970 00:00.
GetTickCount() on Windows will give the time, in milliseconds, since the system's start-up time, but is limited to 49.7 days (after that it simply wraps back to zero).
I want to get the current time, or ticks since system/app start-up time, in milliseconds.
The biggest concern is the method's overhead - I need the lightest one, because I'm about to call it many many times per second.
My case is that I have a worker thread, and to that worker thread I post pending jobs. Each job has an "execution time". So, I don't care if the time is the current "real" time or the time since the system's uptime - it just must be linear and light.
Edit:
unsigned __int64 GetTickCountEx()
{
static DWORD dwWraps = 0;
static DWORD dwLast = 0;
DWORD dwCurrent = 0;
timeMutex.lock();
dwCurrent = GetTickCount();
if(dwLast > dwCurrent)
dwWraps++;
dwLast = dwCurrent;
unsigned __int64 timeResult = ((unsigned __int64)0xFFFFFFFF * dwWraps) + dwCurrent;
timeMutex.unlock();
return timeResult;
}
For timing, the current Microsoft recommendation is to use QueryPerformanceCounter
& QueryPerformanceFrequency
.
This will give you better-than-millisecond timing. If the system doesn't support a high-resolution timer, then it will default to milliseconds (the same as GetTickCount
).
Here is a short Microsoft article with examples of why you should use it :)
这篇关于最快的时序解析系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!