将浮点数乘以整数或另一个浮点数更快吗
In general, float * float
更快,但我怀疑差别很小或没有差别。程序的速度是整个代码的结果,而不仅仅是这一行。这里更快可能在其他地方要多花一点钱。
相信您的编译或获得更好的编译器来生成可执行的代码0.5784f * some_int
well.
In the 0.5784f * some_int
case, the language obliges some_int
to act as if it is converted to float
first*1 before the multiplication. But a sharp compiler may known of implementation specific tricks to perform the multiplications better/faster directly without a separate explicit conversion - as long as it gets an allowable result..
In the float y = 0.5784f + 0.3412f * x; //Where x contains either 0 or 1 (determined dynamically).
一个编译might也看到这一点并利用它来发出高效的代码。
只有在特定的情况下并且凭借经验,您才能猜出编译器。首先代码要清晰。
你总是可以profile https://en.wikipedia.org/wiki/Profiling_(computer_programming)不同的代码/编译器选项并进行比较。
提示:根据我的经验,我发现与发布的问题相比,更大的代码视图可以带来更多的性能提升 - 这接近于微观优化 https://softwareengineering.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding.
*1 See FLT_EVAL_METHOD
for other possibilities.