什么是“代码运动” “循环不变码运动”的意思。 [英] What does "code motion" mean for "loop-invariant code motion"?

查看:127
本文介绍了什么是“代码运动” “循环不变码运动”的意思。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译器中,短语循环不变代码运动描述表达式或语句在循环中的代码不会从迭代改变到迭代,因此可以移动到要被计算一次的循环之外。



我理解短语的循环不变部分,但代码运动是什么意思?



这里有一个例子:

  for(int x = 0; x< string.length(); x ++){
//其他代码b}

如果编译器知道循环中没有任何东西改变字符串的长度,将字符串的长度硬编码到程序中,而不是在适当的字符串上插入对方法 length()的实际调用,因为方法调用将总是返回相同的结果只是浪费内存和处理器时间。方法调用的代码在循环之前移动,而不是保留在循环内部。文章称这种代码运动,虽然我只是调用它原来的优化,因为大多数优化涉及移动代码。 :D


In compilers, the phrase "loop-invariant code motion" describes expressions or statements of code in a loop that don't change from iteration to iteration and hence can be moved outside of the loop to be computed once.

I understand the "loop-invariant" piece of the phrase, but what does the "code motion" mean?

解决方案

The "code motion" just means that the code is moved out of the loop as it won't have any difference if it is performed inside the loop repeatedly or outside the loop once. The compiler is taking the code that doesn't need to be in the loop and moving it outside of it for optimization purposes.

Here an example:

for ( int x=0; x < string.length(); x++) {
    //other code here
}

If the compiler knows that nothing in the loop changes the length of the string, it can just hard-code the length of the string into the program instead of inserting an actual call to the method length() on the appropriate string, because the method call will always return the same result and just waste memory and processor time. The code for the method call is moved before the loop instead of remaining inside it. The article calls this 'code motion', although I'd just call it plain old optimization, as most optimization involves moving code. :D

这篇关于什么是“代码运动” “循环不变码运动”的意思。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆