UNIX编程。结构时间如何打印它(c编程)

时间:2022-12-17 15:03:59

I am trying to print a value of type timeval. Actually I am able to print it, but I get the following warning:

我正在尝试打印一个timeval类型的值。实际上我可以打印出来,但是我得到以下警告:

Multiple markers at this line

这条线上有多个标记

  • format ‘%ld’ expects type ‘long int’, but argument 2 has type ‘struct timeval’
  • 格式' %ld '预期类型为' long int ',但参数2的类型为' 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;

4 个解决方案

#1


22  

In the GNU C Library, struct timeval:

在GNU C库中,struct timeval:

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

声明在sys /时间。h并拥有以下成员:

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.

获取“格式良好”的时间戳,如1.000123。

#2


7  

Since struct timeval will be declared something like:

由于struct timeval将被声明为:

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

#3


1  

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;

}

#4


1  

Yes , timeval is defined like this

是的,timeval是这样定义的

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.

一定会帮助。

#1


22  

In the GNU C Library, struct timeval:

在GNU C库中,struct timeval:

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

声明在sys /时间。h并拥有以下成员:

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.

获取“格式良好”的时间戳,如1.000123。

#2


7  

Since struct timeval will be declared something like:

由于struct timeval将被声明为:

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

#3


1  

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;

}

#4


1  

Yes , timeval is defined like this

是的,timeval是这样定义的

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.

一定会帮助。