- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
struct timeval
和 struct timespec
除了精度不同之外还有什么区别?如果我需要的精度低于 µs(例如,毫秒),我为什么要使用一个而不是另一个?
在我的编译器上(ARM 的 gcc):
/* POSIX.1b structure for a time value. This is like a `struct timeval' but
has nanoseconds instead of microseconds. */
struct timespec
{
__time_t tv_sec; /* Seconds. */
__syscall_slong_t tv_nsec; /* Nanoseconds. */
};
/* A time value that is accurate to the nearest
microsecond but also has a range of years. */
struct timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* Microseconds. */
};
将 __syscall_slong_t
和 __suseconds_t
都定义为“长字”。
最佳答案
我认为这实际上只是 API [in] 兼容性的问题。 POSIX-y 调用类似 pselect()
和 clock_gettime()
使用 struct timespec
。各种文件系统调用,如 utimes()
,以及一些类似 gettimeofday()
的 Linux 调用和 select()
, 使用 struct timeval
。从几个手册页中大致概括,我怀疑 struct timeval
具有 BSD 遗留而 struct timespec
是 POSIX。
如果您正在进行间隔测量,则没有理由不利用 clock_gettime()
的额外精度 — 尽管请注意通常是硬件而非头文件限制了您的测量精度。出于显示目的而除以一百万与除以一千相比几乎没有好坏之分。 (另外,Mac OS X prior to 10.12 did not support clock_gettime()
。)
但如果您要进行大量文件时间操作,则使用 utimes()
等 API 中使用的 struct timeval
可能更有意义。 struct timeval
在 Linux、BSD 和 Mac OS X 上也有一些比较函数,例如timercmp()
、timersub()
(再次参见手册页)。
我会根据您打算使用的 API 做出决定,而不是根据结构本身。 (或者在必要时编写一个带有转换方法的包装类。)
关于c timeval 与 timespec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31275131/
我尝试创建一个代码来生成时间戳。我发现了两种不同的结构,但不清楚有什么区别?谁能帮我理解这两种结构有什么区别? struct timespec 和 timespec now? 最佳答案 以下结构: s
我读过很多关于这个的帖子,我可以告诉你(我读过的每篇帖子都有这组假设,所以让我们早点摆脱它): 我已经适本地包含了 time.h 我已经指定了 POSIX 常量和 -std=gnu99 代码: #if
这个问题在这里已经有了答案: Visual Studio error with 'timespec' structure (5 个答案) 关闭 6 年前。 使用 Visual Studio 2015
我看过this解决方案,但我遇到了同样的错误。对于一个类,我们不得不切换到 c99 编译器而不是 gnu,但现在 timespec 似乎被破坏了。除了“timespec 的存储大小未知”之外,我还收到
在 posix 上,可以使用 timespec 来计算准确的时间长度(如秒和毫秒)。不幸的是,我需要使用 Visual Studio 编译器迁移到 Windows。 VS time.h 库没有声明 t
新用户,如果这个解释不够清楚,我很抱歉...我在创建要在多个源文件之间修改/使用的 timespec 变量时遇到了问题。我的程序旨在确定从我的初始程序中执行另一个程序所需的时间,因此我需要在两个源文件
在 posix 上,可以使用 timespec 来计算准确的时间长度(如秒和毫秒)。不幸的是,我需要使用 Visual Studio 编译器迁移到 Windows。 VS time.h 库没有声明 t
比较两个 timespec 值以查看哪个先发生的最佳方法是什么? 下面有没有问题? bool BThenA(timespec a, timespec b) { //Returns true i
在我的项目中,我使用 struct timespec 如下 struct timespec start, end; clock_gettime(CLOCK_REALTIME,&start); /* D
在累积 struct timespec 增量的程序中,我正在执行以下逻辑: struct timespec accu, start, stop; for (...) { // record s
struct timeval 和 struct timespec 除了精度不同之外还有什么区别?如果我需要的精度低于 µs(例如,毫秒),我为什么要使用一个而不是另一个? 在我的编译器上(ARM 的
以下是重现该问题的一个最小示例。对我来说,这段代码看起来很无辜。我怀疑 struct timespc 背后有一些魔法;但是,我找不到任何可以解释它崩溃原因的内容。 #include #include
我希望格式化 ns 和 ns_old,以便我验证是否遵守设置的 inter_arrival(即,如果我给 inter_arrival =10,它应该正确地反射(reflect)在 ns_diff 中)
我有一个 struct timespec我需要转换为 struct timeval 的对象用于 lutimes(...) . 我尝试了以下操作,但是 lutimes()提示。 const struct
我只是在学习如何使用 clock_getres(网络上几乎没有任何关于此的好文档)。我的代码如下所示: timespec now; clock_getres(CLOCK_REALTIME,&now);
myheader.h #ifndef _MYHEAD_ #define _MYHEAD_ #include #include #include #include #include #incl
我正在从 Linux 内核读取(触摸)事件。我想记录这些事件的时间,但我不知道这些事件是作为 timespec 还是 timeval 传递的。谁能指出我正确的方向? 示例代码(从缓冲区读取事件后) s
我正在将我的应用程序从 unix 移植到 windows,但我遇到了困难。在我的应用程序中,我需要找到以微秒为单位的时间(由于它是高精度应用程序,因此整个应用程序在很大程度上依赖于它)。 以前我使用的
这个答案很难找到,我觉得很奇怪。我已经包含了 time.h 和 ctime,但是 vc9 仍然提示未定义的类型“timespec”。我在这里、MSDN 和网络上进行了搜索(即使存在确切的编译器错误),
我正在使用 struct timespec 结构,这里是: struct timespec { time_t tv_sec; /* Seconds
我是一名优秀的程序员,十分优秀!