UNIX 编程。 struct timeval 如何打印它(C 编程)

发布于 2024-08-05 10:15:23 字数 1251 浏览 10 评论 0原文

我正在尝试打印 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 技术交流群。

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

发布评论

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

评论(6

微暖i 2024-08-12 10:15:23

在 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

In the GNU C Library, struct timeval:

is declared in sys/time.h and has
the following members:

long int tv_sec

This represents the number of whole seconds of elapsed time.

long int tv_usec

This is the rest of the elapsed time (a fraction of a second), represented as the number of microseconds. It is always less than one million.

So you will need to do

printf("%ld.%06ld\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);

to get a "nicely formatted" timestamp like 1.000123.

昨迟人 2024-08-12 10:15:23

由于 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);

Since struct timeval will be declared something like:

struct timeval {
    time_t      tv_sec;
    suseconds_t tv_usec;
}

you need to get at the underlying fields:

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);
娇纵 2024-08-12 10:15:23

是的,它的

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;

}

yeah its

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;

}
鲸落 2024-08-12 10:15:23

我刚刚根据上面的信息编写了这个方便的小功能。除了需要时间之外,是独立的。无论您想了解标准输出流中的时间,都可以使用您想要的标签来调用它。

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,它会关闭所有这些。不管有多少,有多少源代码文件。

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.

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);
}

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:

#define TIMEDUMPS

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

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

Comment out the #define TIMEDUMPS and it turns all of them off. No matter how many, in how many source code files.

习ぎ惯性依靠 2024-08-12 10:15:23

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

肯定会有帮助。

Yes , timeval is defined like this

struct timeval { 
    time_t      tv_sec; 
    suseconds_t tv_usec; 
} 

Using

printf ("%ld.%06ld\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec); 

will surely of help.

冬天的雪花 2024-08-12 10:15:23

.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;
}

.tv_sec can be -ve and when this happens, .tv_usec biased (forced to the range [0..1000000), hence:

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