#include<stsdio.h> #include<time.h>
cloct_t start,stop; //clock_t是clock()函数返回的变量类型;
double duration; //记录被测函数运行时间,以秒为单位;
int main() { //不在测试范围内的准备工作写在clock()调用之前; start=clock(); MyFunction(); stop=clock(); duration=((double)(stop-start))/CLK_TCK; //其他不在测试范围的处理写在后面,例如输出duration的值; return 0; } 如果一个函数跑一次太快,跑不到一个tick,让被测函数重复运行充分多次,使得测出的总的时钟打点间隔充分长,最后计算被测函数平均每次运行的时间即可!