我想要在我的C程序中获得一些基本的时间。 基本上我想在规定的时间内每秒翻几次。 我的问题是师似乎不能正确使用变量的 clock_t
类型。clock_t除法结果为0
下面是一个最小的程序,它说明了什么问题,我都 GCC和铛测试它:
#include <time.h>
#include <stdio.h>
#define CLOCKS_PER_BLINK CLOCKS_PER_SEC/4L
int main() {
printf("%li\n", CLOCKS_PER_SEC);
printf("%li\n", CLOCKS_PER_BLINK);
printf("%li\n", 4L);
printf("%li\n", CLOCKS_PER_SEC/CLOCKS_PER_BLINK);
}
这里是输出:
$ ./a.out
1000000
250000
4
0
我本来期望在最后行返回4
。
注意:'CLOCKS_PER_SEC'不一定是'long'。它是'clock_t'类型,可以是'int','double',...'printf(“%li \ n”,(long)CLOCKS_PER_SEC);''或'printf(“%lli \ n”,(long长)CLOCKS_PER_SEC);'更好。 – chux 2014-09-30 18:04:31