#include #pragma warning(disable:4996)
#define MAXN 10
#define MAXK 1e4
//代表10的7次方
/*f(x)=a0 +a1*x+a2*x*x+a3*x*x*x +...+an*x的N次方 */
clock_t start,stop;
/*clock_t是clock()函数返回的变量的类型*/
double duration;
/*记录被测函数运行的时间,以秒为单位*/
double f1(int n, double a[], double x){
int i;
double p = a[0];
for (i = 1; i <= n; i++){
p += a[i] * pow(x, i);
}
return p;
}
/* 秦九韶算法 */
double f2(int n, double a[], double x){
int i;
double p = a[n];
for (i = n; i > 0; i--){
p = a[i - 1] + x*p;
}
return p;
}
int _tmain(int argc, _TCHAR* argv[])
{
/*不在测试范围内的准备函数,写在开始之前*/
int i;
double a[MAXN];
for (i = 0; i < MAXN; i++)a[i] = (double)i;
start = clock();
for (i = 0; i < MAXK;i++)
f1(MAXN - 1, a, 1.1);
stop = clock();
duration = ((double)(stop - start)) / CLK_TCK / MAXK;
/*其他不在测试范围外的处理写在后面*/
printf("ticks1 = %f\n", (double)(stop - start));
printf("duration1 = %6.2e\n", duration);
start = clock();
for (i = 0; i < MAXK;i++)
f2(MAXN - 1, a, 1.1);
stop = clock();
duration = ((double)(stop - start)) / CLK_TCK/MAXK;
/*其他不在测试范围外的处理写在后面*/
printf("ticks2 = %f\n", (double)(stop - start));
printf("duration2 = %6.2e\n", duration);
/*%6.2e 表示6表示输出长度(包含小数点),2表示输出小数点位数,
d表示输出为十进制
e表示用科学计数法表示*/
return 0;
}