UNIX 编程。 struct timeval 如何打印它(C 编程)
我正在尝试打印 timeval 类型的值。实际上我可以打印它,但我收到以下警告:
此行格式'%ld'处的多个标记
- 需要类型'long int',但参数2具有类型'struct timeval'
程序编译并打印值,但我想知道我是否做错了什么。谢谢。
printf("%ld.%6ld\n",usage.ru_stime);
printf("%ld.%6ld\n",usage.ru_utime);
其中用法是类型
typedef struct{
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
}rusage;
struct rusage usage;
I am trying to print a value of type timeval. Actually I am able to print it, but I get the following warning:
Multiple markers at this line
- format ‘%ld’ expects type ‘long int’, but argument 2 has type ‘struct timeval’
The program compiles and it prints the values, but I would like to know if I am doing something wrong. Thanks.
printf("%ld.%6ld\n",usage.ru_stime);
printf("%ld.%6ld\n",usage.ru_utime);
where usage is of type
typedef struct{
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
}rusage;
struct rusage usage;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 GNU C 库中,
struct timeval :
因此,您需要
获得一个“格式良好”的时间戳,例如
1.000123
。In the GNU C Library,
struct timeval
:So you will need to do
to get a "nicely formatted" timestamp like
1.000123
.由于 struct timeval 的声明类似于:
您需要获取底层字段:
Since
struct timeval
will be declared something like:you need to get at the underlying fields:
是的,它的
yeah its
我刚刚根据上面的信息编写了这个方便的小功能。除了需要时间之外,是独立的。无论您想了解标准输出流中的时间,都可以使用您想要的标签来调用它。
典型的输出如下:
dpyfunc 得到 ftqmut 时间戳:1537394319.501560
并且,您可以用 #ifdef 包围对它的调用,通过注释掉 #define 来一次性打开和关闭它们。这几乎就像分析一样有用,但您可以快速禁用它以用于生产/发布代码。例如:
注释掉#define TIMEDUMPS,它会关闭所有这些。不管有多少,有多少源代码文件。
I just made up this handy little function from the info above. Self-contained except for needing time.h. Call it with the label you want wherever you want to know the time in your stdout stream.
Typical output looks like:
dpyfunc got ftqmut timestamp: 1537394319.501560
And, you can surround the calls to it with a #ifdef to turn them on and off all at once by commenting out your #define. This could be useful almost like profiling but you can quickly disable it for production/release code. Like:
Comment out the #define TIMEDUMPS and it turns all of them off. No matter how many, in how many source code files.
是的,timeval是这样定义的,
使用起来
肯定会有帮助。
Yes , timeval is defined like this
Using
will surely of help.
.tv_sec 可以是 -ve ,当发生这种情况时, .tv_usec 会产生偏差(强制范围为 [0..1000000),因此:
.tv_sec can be -ve and when this happens, .tv_usec biased (forced to the range [0..1000000), hence: