c++ - CLOCKS_PER_SEC 的类型

标签 c++ time types

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_tdouble在您的程序中使用,这样您的代码就可以工作,而不管实际 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 allows clock_t and CLOCKS_PER_SEC to be either floating-point or integral.

您可能会考虑这是否适合您的目的。

关于c++ - CLOCKS_PER_SEC 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6129029/

相关文章:

c++ - 基于范围的for循环的奇怪问题

c - 编译-std=c90时,如何启用CYGWIN中<time.h>中的clock_gettime()声明?

scala - 为什么 Future[Set[Unit]] 不被接受为 Future[Unit]?

scala - Scala 中 int 的 += 方法在哪里

C++ 函数参数数据类型没有名称

c++ - 用前导零进行字符串化

c++ - 通用类对象数组 C++

c++ - 修改boost多索引容器项

mysql - 如何让Mysql的DATE_FORMAT()显示超过24小时的时间?

php - 我想显示时间间隔的时间拾取