如何将 time_t 变量打印为浮点数?
我在C(openMP环境)中使用time_t变量来保持cpu执行时间...我定义了一个浮点值sum_tot_time来总结所有cpu的时间...我的意思是sum_tot_time是cpu的time_t值的总和。问题是打印值 sum_tot_time 它显示为整数或长整型,顺便说一下,没有小数部分!
我尝试了以下方式:
- 将 printf sum_tot_time 作为双精度值
- 将 printf sum_tot_time 作为浮点值 作为浮点值
- 将 printf sum_tot_time 作为双精度值作为 time_t 值
- 将 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:
- to printf sum_tot_time as a double being a double value
- to printf sum_tot_time as float being a float value
- to printf sum_tot_time as double being a time_t value
- to printf sum_tot_time as float being a time_t value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在大多数平台上,
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
:相应地,您可以使用
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 todouble
.)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 anrusage
structure (provided your platform supports it) with user/kernel times, or if usinggettimeofday()
to get wall time, then use both thetv_sec
andtv_usec
fields ofstruct timeval
to generate adouble
value (of millisecond-or-better resolution, typically), and use that instead oftime_t
in your calculations:Correspondingly, you can use
GetThreadTimes
/GetProcessTimes
for user/kernel times or_ftime
for wall time on Windows platforms, then combineFILETIME::dwHighDateTime/dwLowDateTime
.我不确定您是否有权访问标准 *nix 系统调用(或者这是否与您正在做的事情相关),但如果您这样做,您可以使用
timeval struct
和 <代码>gettimeofday。例如,打印出具有六位小数精度的时间戳,从而生成 tcpdump 样式时间戳(由 Steven UNP 提供)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
andgettimeofday
. For example, to print out a timestamp with six decimal places of precision which produces a tcpdump style time stamp ( courtesy of Steven UNP )