CLOCKS_PER_SEC 通常表示为什么数据类型? long unsigned int
? 时钟时间
?它是否因实现而异?
我问是因为我在返回值中使用了 CLOCKS_PER_SEC
,我想确保我使用了最合适的类型。
最佳答案
C 标准所 promise 的是 CLOCKS_PER_SEC
是类型为 clock_t
的常量表达式必须是算术类型(可以是整型,也可以是浮点型)。
(C99 7.23 日期和时间 <time.h>
)
我认为 clock_t
通常是 long
,但我不会用我的生命来打赌我是对的。
我通常信任的 Harbison & Steele(第 3 版)建议类型转换 clock_t
至 double
在您的程序中使用,这样您的代码就可以工作,而不管实际 clock_t
类型(18.1 CLOCK、CLOCK_T、CLOCKS_PER_SEC、TIMES):
Here is how the
clock
function can be used to time an ANSI C program:#include <time.h> clock_t start, finish, duration; start = clock(); process(); finish = clock(); printf("process() took %f seconds to execute\n", ((double) (finish - start)) / CLOCKS_PER_SEC );
Note how the cast to type
double
allowsclock_t
andCLOCKS_PER_SEC
to be either floating-point or integral.
您可能会考虑这是否适合您的目的。
关于c++ - CLOCKS_PER_SEC 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6129029/