如何将 time_t 变量打印为浮点数?

发布于 2024-08-28 16:53:03 字数 364 浏览 8 评论 0原文

我在C(openMP环境)中使用time_t变量来保持cpu执行时间...我定义了一个浮点值sum_tot_time来总结所有cpu的时间...我的意思是sum_tot_time是cpu的time_t值的总和。问题是打印值 sum_tot_time 它显示为整数或长整型,顺便说一下,没有小数部分!

我尝试了以下方式:

  1. 将 printf sum_tot_time 作为双精度值
  2. 将 printf sum_tot_time 作为浮点值 作为浮点值
  3. 将 printf sum_tot_time 作为双精度值作为 time_t 值
  4. 将 printf sum_tot_time 作为浮点值作为 time_t 值

I'm using a time_t variable in C (openMP enviroment) to keep cpu execution time...I define a float value sum_tot_time to sum time for all cpu's...I mean sum_tot_time is the sum of cpu's time_t values. The problem is that printing the value sum_tot_time it appear as an integer or long, by the way without its decimal part!

I tried in these ways:

  1. to printf sum_tot_time as a double being a double value
  2. to printf sum_tot_time as float being a float value
  3. to printf sum_tot_time as double being a time_t value
  4. to printf sum_tot_time as float being a time_t value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

如果没有你 2024-09-04 16:53:03

在大多数平台上,time_t 的分辨率最多为一秒。也就是说,在大多数平台上,time_t 是一个整数(32 位或 64 位)值,用于计算自 1970 年 1 月 1 日午夜 (UTC) 以来经过的秒数,并且只能达到一秒的分辨率。

因此,time_t 值的总和也仅表现出一秒的分辨率(没有小数部分,即使在转换为 double 后也是如此。)

如上所述,您使用什么本机或 OpenMP 调用来获取您尝试累积的 time_t 值?

如果使用本机 *nix getrusage() 调用来填写 rusage 结构(前提是您的平台支持) )与用户/内核时间,或者如果使用 gettimeofday() 获取挂机时间,然后使用 struct timeval 的 tv_sec 和 tv_usec 字段生成一个 double值(通常为毫秒或更好的分辨率),并在计算中使用该值代替 time_t

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

相应地,您可以使用 GetThreadTimes/GetProcessTimes 用于用户/内核时间或 _ftime 用于 Windows 平台上的挂起时间,然后组合 FILETIME::dwHighDateTime/dwLowDateTime

The resolution of time_t is at most one second on most platforms. That is, on most platforms, time_t will be an integer (32- or 64-bit) value counting the number of seconds elapsed since midnight of Jan 1st 1970 (UTC), and can only achieve one-second resolution.

Therefore, a sum of time_t values will also only exhibit one-second resolution (no decimal part, even after converting to double.)

The above having been said, what native or OpenMP call are you using to obtain the time_t values that you are attempting to accumulate?

If using either the native *nix getrusage() call to fill out an rusage structure (provided your platform supports it) with user/kernel times, or if using gettimeofday() to get wall time, then use both the tv_sec and tv_usec fields of struct timeval to generate a double value (of millisecond-or-better resolution, typically), and use that instead of time_t in your calculations:

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

Correspondingly, you can use GetThreadTimes/GetProcessTimes for user/kernel times or _ftime for wall time on Windows platforms, then combine FILETIME::dwHighDateTime/dwLowDateTime.

因为看清所以看轻 2024-09-04 16:53:03

我不确定您是否有权访问标准 *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);
}

I'm not sure if you have access to standard *nix system calls ( or if this is relevant to specifically to what you're doing ), but if you do you can use the timeval struct and gettimeofday. For example, to print out a timestamp with six decimal places of precision which produces a tcpdump style time stamp ( courtesy of 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文