我如何在C中测量时间?
问题描述:
我想知道某个代码块执行了多长时间(大约).像这样:
I want to find out for how long (approximately) some block of code executes. Something like this:
startStopwatch();
// do some calculations
stopStopwatch();
printf("%lf", timeMesuredInSeconds);
怎么样?
答
你可以使用time.h
You can use the clock
method in time.h
示例:
clock_t start = clock();
/*Do something*/
clock_t end = clock();
float seconds = (float)(end - start) / CLOCKS_PER_SEC;