verticesInnerSides.Length / 2
每次以完整的形式在下面的代码之间编写代码的最佳方法是:
for (int i = 0; i < verticesRoof.Length; i++) {
verticesInnerSides[i * NB_VERTICES + verticesInnerSides.Length / 2] = startInside;
verticesInnerSides[i * NB_VERTICES + verticesInnerSides.Length / 2 + 1] = startInside + Vector3.up * HEIGHT_ROOF;
verticesInnerSides[i * NB_VERTICES + verticesInnerSides.Length / 2 + 2] = endInside;
verticesInnerSides[i * NB_VERTICES + verticesInnerSides.Length / 2 + 3] = endInside + Vector3.up * HEIGHT_ROOF;
}
或以下在循环外编写一次的代码:
myCalculatedVar = verticesInnerSides.Length / 2;
for (int i = 0; i < verticesRoof.Length; i++) {
verticesInnerSides[i * NB_VERTICES + myCalculatedVar] = startInside;
verticesInnerSides[i * NB_VERTICES + myCalculatedVar + 1] = startInside + Vector3.up * HEIGHT_ROOF;
verticesInnerSides[i * NB_VERTICES + myCalculatedVar + 2] = endInside;
verticesInnerSides[i * NB_VERTICES + myCalculatedVar + 3] = endInside + Vector3.up * HEIGHT_ROOF;
}
C#编译器会在第一种情况下优化此操作吗?
我认为达到可读性和性能的最佳方法是:
myCalculatedVar = verticesInnerSides.Length / 2;
for (int i = 0; i < verticesRoof.Length; i++) {
int ind = i * NB_VERTICES + myCalculatedVar;
verticesInnerSides[ind] = startInside;
verticesInnerSides[ind + 1] = startInside + Vector3.up * HEIGHT_ROOF;
verticesInnerSides[ind + 2] = endInside;
verticesInnerSides[ind + 3] = endInside + Vector3.up * HEIGHT_ROOF;
}
但是您可能会浪费时间,并编写一些基准测试来测试这一部分.我确信,如果存在瓶颈,它将位于代码的另一个位置.
您为什么要像第一个代码示例那样来做呢?第二个(无需查看编译器)似乎更有效.
根据《代码完成》和许多其他建议:如果不需要,请不要进行优化.
如果需要,只需进行基准测试即可.(关于基准测试时需要注意的内容,有很多主题,博客和答案,但是我相信您将能够找到它们:)
作为额外的奖励,这是您需要的所有链接的另一个答案:)
What is the best approach between the code below where verticesInnerSides.Length / 2
is written in it's complete form each time :
for (int i = 0; i < verticesRoof.Length; i++) {
verticesInnerSides[i * NB_VERTICES + verticesInnerSides.Length / 2] = startInside;
verticesInnerSides[i * NB_VERTICES + verticesInnerSides.Length / 2 + 1] = startInside + Vector3.up * HEIGHT_ROOF;
verticesInnerSides[i * NB_VERTICES + verticesInnerSides.Length / 2 + 2] = endInside;
verticesInnerSides[i * NB_VERTICES + verticesInnerSides.Length / 2 + 3] = endInside + Vector3.up * HEIGHT_ROOF;
}
Or the following where it's written outside the loop once :
myCalculatedVar = verticesInnerSides.Length / 2;
for (int i = 0; i < verticesRoof.Length; i++) {
verticesInnerSides[i * NB_VERTICES + myCalculatedVar] = startInside;
verticesInnerSides[i * NB_VERTICES + myCalculatedVar + 1] = startInside + Vector3.up * HEIGHT_ROOF;
verticesInnerSides[i * NB_VERTICES + myCalculatedVar + 2] = endInside;
verticesInnerSides[i * NB_VERTICES + myCalculatedVar + 3] = endInside + Vector3.up * HEIGHT_ROOF;
}
Will C# compiler optimize this operation in the first case ?
In my opinion the best way to reach readability and performance is:
myCalculatedVar = verticesInnerSides.Length / 2;
for (int i = 0; i < verticesRoof.Length; i++) {
int ind = i * NB_VERTICES + myCalculatedVar;
verticesInnerSides[ind] = startInside;
verticesInnerSides[ind + 1] = startInside + Vector3.up * HEIGHT_ROOF;
verticesInnerSides[ind + 2] = endInside;
verticesInnerSides[ind + 3] = endInside + Vector3.up * HEIGHT_ROOF;
}
But you can waste your time and write some benchmarks test for testing this piece. I am sure that if a bottleneck will exist it will be in another place in the code.
Why would you do it like the first code example to begin with ? The second one (without looking at the compiler) seems more efficient.
As per Code Complete, and many other suggestions: Don't optimize if you don't need to.
If you need to, simply benchmark. (There's plenty of threads, blogs and answers about what to look out for when benchmarking, but I'm sure you'll be able to find them :)
As an extra bonus, here's another answer with all the links you'll need :)
本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文