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;
Answer 1:
在GNU C库 , struct timeval
:
在sys / time.h中声明,并具有下列成员:
long int tv_sec
这代表所耗时整秒数。
long int tv_usec
这是所经过的时间的其余部分(第二的一小部分),表示为微秒数。 它始终是不到一百万。
所以,你需要做的
printf("%ld.%06ld\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
得到一个“格式良好的”时间戳像1.000123
。
Answer 2:
由于struct timeval
将被宣布是这样的:
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
}
你需要获取基础字段:
printf ("%ld.%06ld\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
printf ("%ld.%06ld\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
Answer 3:
是的其
int main( void )
{
clock_t start, stop;
long int x;
double duration;
static struct timeval prev;
struct timeval now;
start = clock(); // get number of ticks before loop
for( x = 0; x < 1000000000; x++ );
// sleep(100);
stop = clock(); // get number of ticks after loop
// calculate time taken for loop
duration = ( double ) ( stop - start ) / CLOCKS_PER_SEC;
printf( "\nThe number of seconds for loop to run was %.2lf\n", duration );
gettimeofday(&now, NULL);
prev.tv_sec = duration;
if (prev.tv_sec)
{
int diff = (now.tv_sec-prev.tv_sec)*1000+(now.tv_usec-prev.tv_usec)/1000;
printf("DIFF %d\n",diff);
}
return 0;
}
Answer 4:
是的,timeval中这样定义
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
}
运用
printf ("%ld.%06ld\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
必将帮助。
Answer 5:
我只是从上面的信息由这个方便的小功能。 自包含的,除了需要time.h中 无论你想知道你的标准输出流的时间和你想要的标签调用它。
void timestamp(char *lbl) { // just outputs time and label
struct timeval tval;
int rslt;
rslt = gettimeofday(&tval,NULL);
if (rslt) printf("gettimeofday error\n");
printf("%s timestamp: %ld.%06ld\n", lbl, tval.tv_sec, tval.tv_usec);
}
典型的输出如下:dpyfunc了ftqmut时间戳:1537394319.501560
而且,您可以围绕它的通话用的#ifdef被注释掉的#define你一次全部打开,并关闭。 这可能是有用的几乎一样,但轮廓可以快速禁用它制作/发行代码。 喜欢:
#define TIMEDUMPS
#ifdef TIMEDUMPS
timestamp("function 1 start");
#endif
#ifdef TIMEDUMPS
timestamp("function 2 start");
#endif
注释掉的#define TIMEDUMPS它原来他们都关闭。 不管有多少,在源代码中有多少个文件。
Answer 6:
.tv_sec可以-ve并且当发生这种情况,.tv_usec偏置(强制为范围[0..1000000),因此:
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
static void frac(intmax_t usec)
{
int precision = 6;
while (usec % 10 == 0 && precision > 1) {
precision--;
usec = usec / 10;
}
printf(".%0*jd", precision, usec);
}
static void print_timeval(struct timeval tv)
{
struct timeval d;
/*
* When .tv_sec is -ve, .tv_usec is biased (it's forced to the
* range 0..1000000 and then .tv_sec gets adjusted). Rather
* than deal with that convert -ve values to +ve.
*/
if (tv.tv_sec < 0) {
printf("-");
struct timeval zero = {0,};
timersub(&zero, &tv, &d);
} else {
d = tv;
}
printf("%jd", (intmax_t)d.tv_sec);
if (d.tv_usec > 0) {
frac(d.tv_usec);
}
}
int main()
{
for (intmax_t i = 1000000; i > 0; i = i / 10) {
for (intmax_t j = 1000000; j > 0; j = j / 10) {
struct timeval a = { .tv_sec = i / 1000000, .tv_usec = i % 1000000, };
struct timeval b = { .tv_sec = j / 1000000, .tv_usec = j % 1000000, };
struct timeval d;
timersub(&a, &b, &d);
printf("%7jd us - %7jd us = %7jd us | %2jd.%06jd - %2jd.%06jd = %2jd.%06jd | ",
i, j, i - j,
(intmax_t)a.tv_sec, (intmax_t)a.tv_usec,
(intmax_t)b.tv_sec, (intmax_t)b.tv_usec,
(intmax_t)d.tv_sec, (intmax_t)d.tv_usec);
print_timeval(d);
printf("\n");
}
}
return 0;
}
文章来源: UNIX Programming. struct timeval how to print it (C-programming)