1 Star 0 Fork 1

Chobits/C Code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
timing_calc_polynomial.c 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
Chobits 提交于 2023-03-16 19:32 . refine [rst].c code
#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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ChobitsY/C-Code.git
git@gitee.com:ChobitsY/C-Code.git
ChobitsY
C-Code
C Code
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385