How to print the variable time_t as a floating point number?

I use the time_t variable in C (openMP enviroment) to save processor runtime ... I define the float sum_tot_time value to sum the time for all processors ... I mean sum_tot_time is the sum of the cpu time_t values. The problem is that when printing the value of sum_tot_time, it appears as an integer or long, by the way, without its decimal part!

I tried as follows:

  • for printf sum_tot_time since double is a double value
  • for printf sum_tot_time as a float being a float value
  • for printf sum_tot_time as the double value of time_t
  • for printf sum_tot_time, since float is a time_t value
+3
source share
2 answers

Resolution time_tis not more than one second on most platforms. That is, on most platforms it time_twill be an integer (32- or 64-bit) value that counts the number of seconds that have passed since midnight January 1, 1970 (UTC), and can reach only one second.

Therefore, the sum of the values time_twill also show only one second resolution (without the decimal part, even after converting to double.)

As stated above, which native or OpenMP call do you use to get the values time_tyou are trying to copy?

* nix getrusage(), rusage ( ), user/kernel times, gettimeofday(), , tv_sec, tv_usec struct timeval, double ( , ) time_t:

struct timeval {
        time_t          tv_sec;         /* seconds */
        suseconds_t     tv_usec;        /* microseconds */
};

, GetThreadTimes/GetProcessTimes / _ftime Windows, FILETIME::dwHighDateTime/dwLowDateTime.

+3

, * nix ( , ), , timeval struct gettimeofday. , , tcpdump ( ​​Steven UNP)

#include    "unp.h"
#include    <time.h>

char *
gf_time(void)
{
    struct timeval  tv;
    time_t          t;
    static char     str[30];
    char            *ptr;

    if (gettimeofday(&tv, NULL) < 0)
        err_sys("gettimeofday error");

    t = tv.tv_sec;  /* POSIX says tv.tv_sec is time_t; some BSDs don't agree. */
    ptr = ctime(&t);
    strcpy(str, &ptr[11]);
        /* Fri Sep 13 00:00:00 1986\n\0 */
        /* 0123456789012345678901234 5  */
    snprintf(str+8, sizeof(str)-8, ".%06ld", tv.tv_usec);

    return(str);
}
0

Source: https://habr.com/ru/post/1740950/


All Articles