gpt4 book ai didi

c++ - CLOCKS_PER_SEC 的类型

转载 作者:可可西里 更新时间:2023-11-01 18:26:52 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com