【发布时间】:2017-04-10 20:16:34
【问题描述】:
我已经编写了这段代码,但它没有返回正确的时间(可能某些代码是错误的)。还有其他方法可以比这个(在c中)更精确地测量两个动作之间的时间吗?
float measureTime(clock_t start, clock_t end){
float totalSeconds;
totalSeconds = ((long double)(end - start))/CLOCKS_PER_SEC;
printf("%.6f", totalSeconds);
return(totalSeconds);
}
int main(){
clock_t start, end;
start = clock();
//piece of code here
//
end = clock();
measureTime(start, end);
return (EXIT_SUCCESS);
}
【问题讨论】:
-
适当的时间是什么意思?
-
什么是
CLOCKS_PER_SEC? -
@MarkYisri,这是
time.h中定义的常量。 -
clock()测量 CPU 时间,而不是实时。 -
...除了 MSVC 的
clock()是非标准的,它测量挂壁时间。