且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是最有效的循环边界检查

更新时间:2023-11-25 15:19:46

编译器可能会看到 objectPtr-> bound 的值不会在迭代。在这种情况下,它会自动将它提取到一个变量,以避免重复查找。

The compiler might see that the value of objectPtr->bound doesn't change between iterations. In this case, it will automatically extract it into a variable to avoid repeated lookups.

没有办法显式告诉编译器表达式的值不更改。

There is no way to explicitly tell the compiler that the value of an expression doesn't change. If the compiler can't detect it itself, you can copy the value into a variable as you did.

将局部变量声明为 const

Declaring local variables as const is not necessary. The compiler can see that you don't assign to the variable in a loop and deduce that its value doesn't change.