转自: http://www.cnblogs.com/NeilHappy/archive/2012/12/08/2808417.html #include <sys/time.h> int gettimeofday(struct timeval *tv,struct timezone *tz); strut timeval { long tv_sec; /* 秒数 */ long tv_usec; /* 微秒数 */ }; gettimeofday将时间保存在结构tv之中.tz一般我们使用NULL来代替。 以下是程序:#i nclude <sys/time.h> #i nclude <stdio.h> #i nclude <math.h>void function() { unsigned int i,j; double y; for(i=0;i<1000;i++) for(j=0;j<1000;j++) y=sin((double)i); } main() { struct timeval tpstart,tpend; float timeuse; gettimeofday(&tpstart,NULL); function(); gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec; //注意毫秒和微妙,写错了输出结果就是零了timeuse/=1000000; printf("Used Time:%f\n",timeuse); exit(0); } //这个程序在我的电脑运行的结果大概为0.03 - 0.04s
时间: 12-21