#include"head_file.h" #include<sys/timeb.h> #include<stdio.h> int main() { timeb t1, t2;//定义两个变量 long t; double x, sum = 0, sum1=1; int i, j, n; printf("请输入x n:"); scanf("%lf %d",&x,&n); ftime(&t1);//记录开始的时间 for (i = 0; i < n; ++i) { sum1 = sum1*(-1/x); sum += sum1; } ftime(&t2);//记录结束的时间 t = (t2.time - t1.time) * 1000 + (t2.millitm - t1.millitm);//计算程序运行的时间 printf("sum is %ld,and time is :%ld\n",sum,t); return 0; }
C/C++可以用的计算程序运行时间的方法,布布扣,bubuko.com
原文地址:http://blog.csdn.net/nyist327/article/details/25002133