代码拉取完成,页面将自动刷新
#include <math.h>
#include <stdio.h>
#include <time.h>
double calc_polynomial_1(double a[], int n, double x);
double calc_polynomial_2(double a[], int n, double x);
// f(x) = a0 + a1*x + a2*x^2 + a3*x^3 + ... + an*x^n
typedef double (*function_t)(double a[], int n, double x);
void time_test(function_t function, const char* message, double a[], int n, double x);
int main(void)
{
double a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
double xs[] = {0, 1.1, 99};
for (int i = 0; i < sizeof(xs) / sizeof(double); i++)
{
printf("f(%.1lf):\n", xs[i]);
time_test(calc_polynomial_1, "Simple loop", a, sizeof(a) / sizeof(double), xs[i]);
time_test(calc_polynomial_2, "Horner algorithm", a, sizeof(a) / sizeof(double), xs[i]);
printf("\n");
}
return 0;
}
double calc_polynomial_1(double a[], int n, double x)
{
double y = 0;
for (int i = 0; i < n; i++)
{
y += a[i] * pow(x, i);
}
return y;
}
double calc_polynomial_2(double a[], int n, double x)
{
double y = 0;
for (int i = n - 1; i >= 0; i--)
{
y = a[i] + x * y;
}
return y;
}
void time_test(function_t function, const char* message, double a[], int n, double x)
{
double result;
clock_t start = clock();
for (int i = 0; i < 1000000; i++)
{
result = function(a, n, x);
}
clock_t end = clock();
printf("result: %lf, duration: %6.2lfms (%s)\n", result, (double)(end - start) / CLOCKS_PER_SEC * 1000, message);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。