20

我正在尝试打印 timeval 类型的值。实际上我可以打印它,但我收到以下警告:

此行有多个标记

  • 格式“%ld”需要类型“long int”,但参数 2 的类型为“struct timeval”</li>

该程序编译并打印值,但我想知道我是否做错了什么。谢谢。

    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;
4

6 回答 6

30

在 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.

于 2009-09-24T02:30:51.753 回答
9

因为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);
于 2009-09-24T02:26:40.097 回答
2

是的

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;

}
于 2010-11-30T11:52:42.087 回答
1

是的,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); 

肯定会有所帮助。

于 2009-10-06T11:56:25.280 回答
1

我只是根据上面的信息组成了这个方便的小功能。自包含除了需要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 got ftqmut timestamp: 1537394319.501560

而且,您可以用#ifdef 将调用包围起来,通过注释掉您的#define 来一次性打开和关闭它们。这几乎像分析一样有用,但您可以快速禁用它以用于生产/发布代码。像:

#define TIMEDUMPS

#ifdef TIMEDUMPS
timestamp("function 1 start");
#endif

#ifdef TIMEDUMPS
timestamp("function 2 start");
#endif

注释掉#define TIMEDUMPS 并将它们全部关闭。不管有多少,在多少个源代码文件中。

于 2018-09-19T21:55:28.630 回答
1

.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;
}
于 2019-05-08T02:28:29.153 回答