Mat*_*ner 28 c python linux time
我正在寻找GetTickCount()
与Linux 相当的东西.
目前我正在使用Python time.time()
,可能是通过调用gettimeofday()
.我担心的是,如果时钟被弄乱,例如NTP,返回的时间(unix时期)可能会不规律地改变.一个简单的过程或系统壁挂时间,只能以恒定的速率正向增加就足够了.
是否存在C或Python中的任何此类时间函数?
nos*_*nos 27
您可以在C中使用CLOCK_MONOTONIC:
struct timespec ts;
if(clock_gettime(CLOCK_MONOTONIC,&ts) != 0) {
//error
}
Run Code Online (Sandbox Code Playgroud)
以Python方式查看此问题 - 如何在python中获得单调持续时间?
小智 6
这似乎有效:
#include <unistd.h>
#include <time.h>
uint32_t getTick() {
struct timespec ts;
unsigned theTick = 0U;
clock_gettime( CLOCK_REALTIME, &ts );
theTick = ts.tv_nsec / 1000000;
theTick += ts.tv_sec * 1000;
return theTick;
}
Run Code Online (Sandbox Code Playgroud)
是的,get_tick()是我的应用程序的主干.每个"任务"由一个状态机组成,例如,可以在不使用线程的情况下进行多任务和进程间通信可以实现非阻塞延迟.