ftime()函數的用法----算函數運行時間

ftime()函數取得目前的時間和日期。函數

 

相關函數:time, ctime, gettimeofday
表頭文件:#include <sys/timeb.h>
函數定義:int ftime(struct timeb *tp);
函數說明:ftime()將目前日期由tp所指的結構返回。tp結構定義:

struct   timeb{
       time_t   time;                      /* 爲1970-01-01至今的秒數*/
       unsigned   short   millitm;   /* 千分之一秒即毫秒 */
       short   timezonel;               /* 爲目前時區和Greenwich相差的時間,單位爲分鐘 */
       short   dstflag;                   /* 爲日光節約時間的修正狀態,若是爲非0表明啓用日光節約時間修正 */
};
spa

通常只用到前兩個數據,例子以下:get

 /* algo1-1.c 計算1-1/x+1/x*x…  */it

 #include<stdio.h>io

 #include<sys/timeb.h>im

 void main()nw

 {數據

   struct timeb t1,t2;di

   long t;文件

   double x,sum=1,sum1;

   int i,j,n;

   printf("請輸入x n:");

   scanf("%lf%d",&x,&n);

   ftime(&t1); /* 求得當前時間 */

   

for(i=1;i<=n;i++)

   {

     sum1=1;

     for(j=1;j<=i;j++)

       sum1=-sum1/x;

     sum+=sum1;

   }

   ftime(&t2); /* 求得當前時間 */

   t=(t2.time-t1.time)*1000+(t2.millitm-t1.millitm); /* 計算時間差 */

   printf("sum=%lf 用時%ld毫秒\n",sum,t);

   system("pause");

 }

本人電腦中運行的一個例子就是當x=3時,n=1000,用時9毫秒。

相關文章
相關標籤/搜索