什么是用printf打印clock_t表示正确的方法是什么? [英] What’s the correct way to use printf to print a clock_t?

查看:2579
本文介绍了什么是用printf打印clock_t表示正确的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用一个显式转换为无符号长长,并使用%LLU 打印,但因为为size_t %Z 说明,为什么不 clock_t表示有吗?

I'm currently using a explicit cast to unsigned long long and using %llu to print it, but since size_t has the %z specifier, why doesn't clock_t have one?

甚至没有为它的宏。也许我可以假设一个x64操作系统(OS和CPU)对为size_t 的长度为8个字节(即使在这种情况下,他们已经提供了% ž),但关于 clock_t表示

There isn't even a macro for it. Maybe I can assume that on an x64 system (OS and CPU) size_t is 8 bytes in length (and even in this case, they have provided %z), but what about clock_t?

推荐答案

有似乎没有完美的方式。问题的根源在于 clock_t表示可以是整数或浮点数。

There seems to be no perfect way. The root of the problem is that clock_t can be either integer or floating point.

clock_t表示可以是一个浮点类型

由于巴斯蒂安莱昂纳尔提到对POSIX(GO给予好评他)的 C99 N1256草案 7.23.1 / 3也说:

As Bastien Léonard mentions for POSIX (go upvote him), C99 N1256 draft 7.23.1/3 also says that:

[clock_t表示是]算术类型能够重新presenting次

[clock_t is] arithmetic types capable of representing times

和6.2.5 / 18:

and 6.2.5/18:

整数和浮点类型统称为算术类型。

Integer and floating types are collectively called arithmetic types.

和标准定义算术类型无论是整数或浮点数类型。

and the standard defines arithmetic type as either integers or floating point types.

如果您将CLOCKS_PER_SEC分割,使用长双

返回值时钟()是实现定义,并获得标准含义出它是除以 CLOCKS_PER_SEC <的唯一途径/ code>来找到的秒数:

The return value of clock() is implementation defined, and the only way to get standard meaning out of it is to divide by CLOCKS_PER_SEC to find the number of seconds:

clock_t t0 = clock();
/* Work. */
clock_t t1 = clock();
printf("%Lf", (long double)(t1 - t0));

这是不够好,虽然并不完美,两个原因如下:

This is good enough, although not perfect, for the two following reasons:


  • 有似乎没有模拟到还会将intmax_t 浮点类型:<一href=\"http://stackoverflow.com/questions/17189423/how-to-get-the-largest-$p$pcision-floating-point-data-type-of-implemenation-and-i\">How得到的实行最大precision浮点数据类型和printf的说明?所以,如果一个更大的浮点类型出来,明天,它可以用来,打破您的实现。

  • there seems to be no analogue to intmax_t for floating point types: How to get the largest precision floating point data type of implemenation and its printf specifier? So if a larger floating point type comes out tomorrow, it could be used and break your implementation.

如果 clock_t表示是一个整数,浮动投是很好定义为使用最近的浮动可能。你可能会失去precision,但它没多大关系相比绝对值,只会发生的巨额时间,例如长整型在86是80位浮点64位显著,这是亿万年以秒为单位。

if clock_t is an integer, the cast to float is well defined to use the nearest float possible. You may lose precision, but it would not matter much compared to the absolute value, and would only happen for huge amounts of time, e.g. long int in x86 is the 80-bit float with 64-bit significant, which is millions of years in seconds.

去给予好评lemonad 谁说过类似的话。

如果你以为这是一个整数,使用%菊和uintmax_t型

虽然无符号长长是目前最大的标准整型可能的:

Although unsigned long long is currently the largest standard integer type possible:

  • a larger one could come out in the future
  • the standard already explicitly allows larger implementation defined types (kudos to @FUZxxl) and clock_t could be one of them

所以最好是强制转换为上最大的无符号整型可能的:

so it is best to typecast to the largest unsigned integer type possible:

#include <stdint.h>

printf("%ju", (uintmax_t)(clock_t)1);

uintmax_t型是保证在机器上最大可能的整数尺寸的大小。

uintmax_t is guaranteed to have the size of the largest possible integer size on the machine.

uintmax_t型和printf的说明%菊在C99引入和gcc例如实现它们。

uintmax_t and its printf specifier %ju were introduced in c99 and gcc for example implements them.

作为奖励,这解决了一劳永逸的所有问题,如何可靠地的printf 整数类型(这是不幸的是没有为 clock_t表示)。

As a bonus, this solves once and for all the question of how to reliably printf integer types (which is unfortunately not the necessarily the case for clock_t).

什么可能出问题,如果它是一个双:

What could go wrong if it was a double:


  • 如果太大,不适合入整数,不确定的行为

  • 远小于1,将获得四舍五入为0,你不会看到任何东西

由于这些后果是严厉得多比整数浮充转换,采用浮动可能是一个更好的主意。

Since those consequences are much harsher than the integer to float conversion, using float is likely a better idea.

在glibc的2.21,是个整数

手册上说,使用双击是一个更好的主意:

The manual says that using double is a better idea:

在GNU / Linux和GNU /赫德系统,clock_t表示等价于long int和CLOCKS_PER_SEC是一个整数值。但在其他系统中,既clock_t表示与宏CLOCKS_PER_SEC可以是整数或浮点类型。铸造CPU时间的值加倍,如在上面的例子中,可以确保诸如算术和打印工作的操作正确和一致不管底层重新presentation是什么。

On GNU/Linux and GNU/Hurd systems, clock_t is equivalent to long int and CLOCKS_PER_SEC is an integer value. But in other systems, both clock_t and the macro CLOCKS_PER_SEC can be either integer or floating-point types. Casting CPU time values to double, as in the example above, makes sure that operations such as arithmetic and printing work properly and consistently no matter what the underlying representation is.

在glibc的2.21:

In glibc 2.21:


  • clock_t表示长整型

  • time/time.h sets it to __clock_t
  • bits/types.h sets it to __CLOCK_T_TYPE
  • bits/typesizes.h sets it to __SLONGWORD_TYPE
  • bits/types.h sets it to long int

时钟()在Linux中实现与 sys_clock_gettime

clock() in Linux is implemented with sys_clock_gettime:

  • sysdeps/unix/sysv/linux/clock.c calls __clock_gettime
  • sysdeps/unix/clock_gettime.c calls SYSDEP_GETTIME_CPU
  • sysdeps/unix/sysv/linux/clock_gettime.c calls SYSCALL_GETTIME which finally makes an inline system call

男人clock_gettime ,告诉我们,它返回一个结构的timespec 这在GCC包含长整型字段。

man clock_gettime, tells us that it returns a struct timespec which in GCC contains long int fields.

所以底层实现真正返回一个整数。

So the underlying implementation really returns integers.

另请参见

  • How to print types of unknown size like ino_t?
  • How to use printf to display off_t, nlink_t, size_t and other special types?

这篇关于什么是用printf打印clock_t表示正确的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆