【发布时间】:2021-11-27 11:46:53
【问题描述】:
编译器是否“足够聪明”来预先计算它,还是会对性能产生影响?
const float num = 0.8660254f; // Sqrt(3) / 2
while(true)
{
float h = num * GetSomeNumber();
}
对
while(true)
{
float h = (Sqrt(3) / 2 ) * GetSomeNumber();
}
对
const float num = 1.7320508f; // Sqrt(3)
while(true)
{
float h = (num / 2) * GetSomeNumber();
}
还有什么理由(不)进行预计算?
【问题讨论】:
-
Sharplab 非常适合这个sharplab.io/…
-
“重复”不是重复。
Math.Sqrt(3)不是常数,至少就 C# 而言。但正确答案是:If you want to know which horse is faster then race your horses.
标签: c#