代码拉取完成,页面将自动刷新
#include <math.h>
#include <stdio.h>
#include <time.h>
float calc_sqrt_1(float x);
float calc_sqrt_2(float x);
float calc_sqrt_3(float x);
typedef float (*function_t)(float x);
void time_test(function_t function, const char* message, float x);
int main(int argc, char const* argv[])
{
float xs[] = {-1, 0, 1, 2, 100, 999999};
for (int i = 0; i < sizeof(xs) / sizeof(float); i++)
{
printf("sqrt(%.1f):\n", xs[i]);
time_test(calc_sqrt_1, "Newton's method", xs[i]);
time_test(calc_sqrt_2, "Magic number", xs[i]);
time_test(calc_sqrt_3, "Standard library", xs[i]);
printf("\n");
}
return 0;
}
float calc_sqrt_1(float x)
{
if (x <= 0)
{
return (x < 0) ? NAN : 0;
}
float res = x;
float tmp;
while (1)
{
tmp = (res + x / res) / 2;
if (tmp == res) // for speed and accuracy
{
return res;
}
res = tmp;
}
return -1; // unreachable
}
float calc_sqrt_2(float x)
{
if (x <= 0)
{
return (x < 0) ? NAN : 0;
}
unsigned int bits = 0;
bits = *((int*)&x);
bits = (bits >> 1) + 0x1fbb4f2e;
return *((float*)&bits);
}
float calc_sqrt_3(float x)
{
return sqrtf(x);
}
void time_test(function_t function, const char* message, float x)
{
float result;
clock_t start = clock();
for (int i = 0; i < 1000000; i++)
{
result = function(x);
}
clock_t end = clock();
printf("result: %f, duration: %6.2lfms (%s)\n", result, (double)(end - start) / CLOCKS_PER_SEC * 1000, message);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。