UNIX Programming. struct timeval how to print it (C-programming)
Asked Answered
R

6

23

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;
Roseola answered 24/9, 2009 at 2:20 Comment(2)
well of course I know timeval is not of type long thats why it gives me the warning, but is there a way to do this properly?Roseola
You guys rock... that worked perfectly...Roseola
L
34

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.

Loadstone answered 24/9, 2009 at 2:30 Comment(2)
On my system, struct timeval is declared with "time_t tv_sec" and "suseconds_t tv_usec". I've had luck printing these with "%ld", but there's no guarantee. It's probably safest to cast to long before printing.Biotechnology
Since struct timeval can represent -ve values this solution won't work in general (look for solutions that handle the bias). Fortunately here both .ru_utime and .ru_stime are both +ve so it's sufficient.Planography
S
10

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);
Stephanus answered 24/9, 2009 at 2:26 Comment(3)
You need to use %06ld - otherwise you get blanks in funny places. Also, you have to worry about casting to long if you use %ld.Phenix
If I wanted to assign the system time to another variable, let's say: long specialTime; is there a way to do it?Roseola
Depends on the resolution you want. I'd opt for just declaring my own "struct timeval myTime;" then using "memcpy (&myTime,&(usage.ru_stime),sizeof(myTime));". Then you can compare it a later ru_stime to get a duration.Stephanus
F
2

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;

}
Frisket answered 30/11, 2010 at 11:52 Comment(2)
For the record, to compute your diff variable, you should use the timersub() macro defined in #include <sys/time.h>. See man 3 timeradd.Tacheometer
@HeisSpiter timersub is not POSIX-compliant and thus not very portable.Bibliophile
D
2

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.

Disport answered 19/9, 2018 at 21:55 Comment(0)
E
1

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.

Espionage answered 6/10, 2009 at 11:56 Comment(0)
P
1

.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;
}
Planography answered 8/5, 2019 at 2:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.