获取自进程启动以来经过的时间

发布于 2024-07-07 09:28:01 字数 347 浏览 8 评论 0 原文

我需要一种方法来获取自程序启动以来经过的时间(挂钟时间),以一种能够适应用户干预系统时钟的方式。

在Windows上,非标准clock()实现并不能解决问题,因为它似乎只是通过计算启动时采样时间的差来工作,因此如果我“将时钟指针向后移动”,我会得到负值。

在 UNIX 上,clock/getrusage 指的是系统时间,而使用 gettimeofday 等函数来采样时间戳与在 Windows 上使用时钟具有相同的问题。

我对精度并不真正感兴趣,并且我通过在后台旋转半秒分辨率的计时器来解决时钟偏差发生时的问题 (如果采样时间和预期时间之间的差异超过 1 秒,我将预期计时器用于新基线)但我认为必须有更好的方法。

I need a way to get the elapsed time (wall-clock time) since a program started, in a way that is resilient to users meddling with the system clock.

On windows, the non standard clock() implementation doesn't do the trick, as it appears to work just by calculating the difference with the time sampled at start up, so that I get negative values if I "move the clock hands back".

On UNIX, clock/getrusage refer to system time, whereas using function such as gettimeofday to sample timestamps has the same problem as using clock on windows.

I'm not really interested in precision, and I've hacked a solution by having a half a second resolution timer spinning in the background countering the clock skews when they happen
(if the difference between the sampled time and the expected exceeds 1 second i use the expected timer for the new baseline) but I think there must be a better way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浮生面具三千个 2024-07-14 09:28:01

我想你总是可以启动某种计时器。 例如Linux下的一个线程
会有这样的循环:

static void timer_thread(void * arg)
{
        struct timespec delay;
        unsigned int msecond_delay = ((app_state_t*)arg)->msecond_delay;

        delay.tv_sec = 0;
        delay.tv_nsec = msecond_delay * 1000000;

        while(1) {
                some_global_counter_increment();
                nanosleep(&delay, NULL);
        }
}

其中 app_state_t 是您存储变量时选择的应用程序结构。 如果你想防止篡改,你需要确保没有人杀死你的线程

I guess you can always start some kind of timer. For example under Linux a thread
that would have a loop like this :

static void timer_thread(void * arg)
{
        struct timespec delay;
        unsigned int msecond_delay = ((app_state_t*)arg)->msecond_delay;

        delay.tv_sec = 0;
        delay.tv_nsec = msecond_delay * 1000000;

        while(1) {
                some_global_counter_increment();
                nanosleep(&delay, NULL);
        }
}

Where app_state_t is an application structure of your choice were you store variables. If you want to prevent tampering, you need to be sure no one killed your thread

仄言 2024-07-14 09:28:01

对于 POSIX,请将 clock_gettime()CLOCK_MONOTONIC 结合使用。

For POSIX, use clock_gettime() with CLOCK_MONOTONIC.

摇划花蜜的午后 2024-07-14 09:28:01

我认为您不会找到跨平台的方法来做到这一点。

在 Windows 上,您需要的是 GetTickCount (或者可能 QueryPerformanceCounterQueryPerformanceFrequency 用于高分辨率计时器)。 我在 Linux 上没有这方面的经验,但是 Google 上的搜索给了我 clock_gettime< /a>.

I don't think you'll find a cross-platform way of doing that.

On Windows what you need is GetTickCount (or maybe QueryPerformanceCounter and QueryPerformanceFrequency for a high resolution timer). I don't have experience with that on Linux, but a search on Google gave me clock_gettime.

与之呼应 2024-07-14 09:28:01

挂钟时间可以通过time()调用来计算。

Wall clock time can bit calculated with the time() call.

时光磨忆 2024-07-14 09:28:01

如果有网络连接,您始终可以从 NTP 服务器获取时间。 这显然不会受到任何本地时钟的影响。

If you have a network connection, you can always acquire the time from an NTP server. This will obviously not be affected in any the local clock.

残龙傲雪 2024-07-14 09:28:01

Linux 上的 /proc/uptime 维护系统已启动的秒数(以及空闲的秒数),这应该不受时钟更改的影响,因为它是由系统中断 (jiffies / HZ) 维护的。 也许windows有类似的东西?

/proc/uptime on linux maintains the number of seconds that the system has been up (and the number of seconds it has been idle), which should be unaffected by changes to the clock as it's maintained by the system interrupt (jiffies / HZ). Perhaps windows has something similar?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文