Linux 上是否有类似 GetTickCount() 的函数?
我已经尝试过其他一些 sutff,但它们根本没有用。
所以它应该返回自启动以来的确切时间(以毫秒为单位)。
/// Returns the number of ticks since an undefined time (usually system startup).
static uint64_t GetTickCountMs()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)(ts.tv_nsec / 1000000) + ((uint64_t)ts.tv_sec * 1000ull);
}
也很有用...
/// Computes the elapsed time, in milliseconds, between two 'timespec'.
inline uint32_t TimeElapsedMs(const struct timespec& tStartTime, const struct timespec& tEndTime)
{
return 1000*(tEndTime.tv_sec - tStartTime.tv_sec) +
(tEndTime.tv_nsec - tStartTime.tv_nsec)/1000000;
}
带有 CLOCK_MONOTONIC 的clock_gettime是您似乎正在寻找的魔法咒语。示例代码,未经测试:
struct timespec *t;
t = (struct timespec *)malloc(sizeof(t));
clock_gettime(CLOCK_MONOTONIC, t);
让我知道你是怎么办的。我目前不在 Linux 上,因此这只是我所指的手册页的推测。
嗯,编译器说它不存在,但我包括了 time.h
它会给你一个编译器或链接器错误吗?如果您收到链接器错误,可能是因为您没有与“rt”库链接。