English 中文(简体)
编篡人如何对待 lo制检查
原标题:how does compiler treat for loop checks
  • 时间:2012-04-04 17:11:15
  •  标签:

如果是这样做的话

for i = 0; i < len(arr); i++ {
    ...
}

透镜是否按每秒计算? 对于电离层,可以设定任何优化旗帜来改变。

问题回答

I believe that the language definition (assuming we re taking C/C++ here) requires that it is invoked every time. As suggested in other answers, you have to do this optimisation yourself. The compiler generally can t know whether calling the len() function will have side effects, or will return the same value on each invocation, so can t generally pull it out of the loop. Since you know what len() does, you can perform that optimisation.

是的,在每一处都援引过透镜。 如果你使用简单的变数,情况就相同。

for( i=0 ; i< a; i++)

虽然不建议你改变“条码”(<>a>/条码>中途的价值。

if it does not change during each iteration. it is called a loop invariant. loop invariants can be (can is the word) be hoisted above the loop body. but it is not necessary to do that for correctness. and in the case of a function call, it will be difficult for the compiler to determine if the value is a loop invariant or not. if it were an inline function, then it would be theoretically possible to determine the loop invariance. but i am not sure if any specific compiler actually does this.

http://en.wikipedia.org/wiki/Loop-invariant_code_motion” rel=“nofollow”>loop invariant codestart

如果你不想要len子,就每当执行:

int length = len(arr);

for i = 0; i < length; i++ {
    ...
}




相关问题
热门标签