2013-04-19 22 views
1

我在尝试使用clock()来确定C中函数的运行时间。 这是迄今为止代码:使用时钟确定函数的运行时间()

time_t start, end; 
start = clock(); 
// Function here 
end = clock(); 
printf("Time was: %lf\n", ((double)(end-start)/CLOCKS_PER_SEC)); 

并返回Time was: 0.030000。如果我添加几秒钟的延迟,则会显示Time was: 0.500000。我怎样才能让它在毫秒内正确显示?例如。 Time was: 500 millisecondsTime was: 30 milliseconds

谢谢!

回答

2

为了通过1000以毫秒为单位显示时间,在乘以秒计的时间:

printf("Time was: %d\n", (1000*(end-start)/CLOCKS_PER_SEC)); 

上面的代码截断时间到最小毫秒。