gpt4 book ai didi

c - sinf() 比 中的 sin() 快?

转载 作者:太空宇宙 更新时间:2023-11-04 03:43:05 25 4
gpt4 key购买 nike

我只是想知道自从我在浮点变量中使用 sin 和 cos 以来我的程序会更快,所以我决定以这种方式进行测试:

int c;
float a;
float b = 0.5;

/***********************/

glfwSetTime(0.0);
time = glfwGetTime();
printf("\n%f", time);

/***********************/

glfwSetTime(0.0);
a = sinf(b);
for (c = 0; c < 10000; c++)
sinf(b);
time = glfwGetTime();
printf("\n%f, %f", time, a);

/***********************/

glfwSetTime(0.0);
a = sin(b);
for (c = 0; c < 10000; c++)
sin(b);
time = glfwGetTime();
printf("\n%f, %f", time, a);

/***********************/

while (1);

这是我得到的输出:

0.000001
0.000505, 0.479426
0.000300, 0.479426

问题是一样的,为什么使用带 float 的 sin() 比使用正确的函数 sinf() 更快?谢谢!

最佳答案

sinf 的某些实现只是对 sin 的简单调用,并在 float 之间进行前后转换。这可能是你的情况。

例如:http://code.metager.de/source/xref/gnu/octave/gnulib-hg/lib/sinf.c

关于c - sinf() 比 <math.h> 中的 sin() 快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26959316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com