C 获取时间 之 timeb.h

timeb.h 中定义了结构体timeb 和 函数 ftime. 可以获得秒和毫秒的时间。

struct timeb
  {
    time_t time;		/* Seconds since epoch, as from `time'.  */
    unsigned short int millitm;	/* Additional milliseconds.  */
    short int timezone;		/* Minutes west of GMT.  */
    short int dstflag;		/* Nonzero if Daylight Savings Time used.  */
  };

/* Fill in TIMEBUF with information about the current time.  */

extern int ftime (struct timeb *__timebuf);

实例:通过两个timeb 结构体获取ms级的执行时间,可以同来统计程序时间复杂度。

 #include<stdio.h>
 #include<sys/timeb.h>
 void main()
 {
   struct timeb t1,t2;
   long t;
   int i;
   ftime(&t1);  /* 获取当前时间 */
   for(i=0;i<100000000;++i);
   ftime(&t2); /* 获取当前时间 */
   t=(t2.time-t1.time)*1000+(t2.millitm-t1.millitm); /* 计算毫秒级的时间 */
   printf("ms time is %ld \n",t);
 }

猜你喜欢

转载自blog.csdn.net/u013894391/article/details/88988862