测试程序运行时间

精确到秒:

time_t t1,t2;

time(&t1);

some instruction.....

time(&t2);

printf("%d\n",t2-t1);

精确到毫秒:

clock_t c1,c2;

c1=clock();

some instruction....

c2=clock();

printf("%d\n",c2-c1);

精确到微妙:

#include <sys/time.h>
#include <unistd.h>

struct timeval tv_start,tv_end;

gettimeofday(&tv_start,NULL);

some instruction....

gettimeofday(&tv_end,NULL);

printf("%d\n",(tv_end.tv_sec-tv_start.tv_sec)*1000000+(tv_end.tv_usec-tv_start.tv_usec));

 

posted @ 2015-03-29 16:25  li-xingtao  阅读(128)  评论(0编辑  收藏  举报