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毫秒。