Key Difference – Machine依赖与机器独立的代码优化
Computer programs are sets of instructions given to硬件,to perform tasks. These programs are mostly written in high-level languages, and the computer does not understand that language. Therefore, acompileris used to convert those instructions into machine code or target code. It goes through several phases to build the target code. Code optimization is one of them. There are two optimization techniques like, machine dependent and machine independent code optimization. The关键区别在机器依赖和机器独立代码优化之间当机器独立代码优化应用于中间代码时,将机器依赖性优化应用于对象代码。
CONTENTS
1。概述和关键差异
2。What is Machine Dependent Code Optimization
3。What is Machine Independent Code Optimization
4。Similarities Between Machine Dependent and Machine Independent Code Optimization
5。并排比较,机器与Mac的依赖hine Independent Code Optimization in Tabular Form
6。Summary
什么是机器依赖代码优化?
将源代码转换为对象代码或目标代码时,编译器会通过多个阶段。First, the source code is given to Lexical analyzer which produces tokens. Then, the output is given to syntax analyzer which investigates whether the generated tokens are in logical order. That output is given to the semantic analyzer. Assume that there is a piece of code as p = q + r;
Here, p, q are integers, but r is a float. Using the semantic analyzer, the c integer variable is converted to a float. Therefore, it does the semantic analyzing. The output of the semantic analyzer goes to the Intermediate code generator. It returns an intermediate code which then goes to code optimizer. Code optimization is the process of eliminating the non-essential program statements without changing the meaning of actual source code. It is not a compulsory optimize but, it can improve the running time of the target code. The output of the code optimizer is given to the code generator, and finally, the target code is built.
在机器依赖的代码优化中,优化应用于源代码。在此优化中分配足够数量的资源可以改善程序的执行。
什么是机器独立代码优化?
在中间代码上进行优化时,称为机器独立代码优化。有不同的技术来实现机器独立的代码优化。使用以下示例来描述它们。
读取代码的波纹管。
for(j = 0; j <10; j ++){
b = x+2;
a[j] = 5* j;
}
According to the above code, b = x+2 is calculated again and again in each iteration. Once b is calculated, it does not change. So, this line can be placed outside the loop as follows.
b = x+2;
for(j = 0; j <10; j ++)
{a [j] = 5 * j;
}
这称为代码移动。
读取代码的波纹管。
j=5;
if(j == 10){
a = b+20;
}
根据上述代码,“如果块”将永远不会执行,因为j值永远不会等于10。它已经初始化为值。因此,如果可以删除块,则此。该技术是消除代码。
Another method is strength reduction. Arithmetic operations like multiplication require morememory,,,,time, and CPU cycles. These expensive expressions can be replaced by cheap expressions like b = a * 2; or can be replaced by addition, b = a + a;
请参阅以下代码。
for(j = 1; j <= 5; j ++){
值= J * 5;
}
Instead of the multiplication, the code can be changed as follows.
int temp = 5;
for(j = 1; j <= 5; j ++){
temp = temp + 5;
值= temp;
}
It is possible to evaluate the expressions which are constants at runtime. It is called恒定折叠。可以说明B [J+1] = C [J+1];
相反,它可以如下更改。
n = j +1;
b [n] = c [n];
可以如下有循环。
for (j=0; j<5; j++) {
printf(“a\n”);
}
for(j = 0; j <5; j ++){
printf(“ b \ n”);
}
打印A和B,都具有相同数量的迭代。两者都可以组合到一个循环中,如下所示。
for(j = 0; j <5; j ++){
printf(“ a \ n”);
printf(“ b \ n”);
}
另一个重要技术是常见的子表达消除。它是用单个变量替换相同的表达式来进行计算。参考波纹管。
a = b*c + k;
d = b* c + m;
This code can be converted as follows.
temp = b*c;
a = temp + k;
d = temp + m;
It is not required to calculate b*c again and again. The multiplied value can be stored in a variable and reused.
What is the Similarity Between Machine Dependent and Machine Independent Code Optimization?
- 这两者都属于代码优化
机器依赖性和机器独立代码优化有什么区别?
机器依赖vs Machine Independent Code Optimization |
|
机器依赖的代码优化应用于对象代码。 | 无关的代码优化应用于中间代码。 |
参与硬件 | |
机器依赖性优化涉及CPU寄存器和绝对内存引用。 | 机器独立的代码优化不涉及CPU寄存器或绝对内存引用。 |
摘要 - 机器依赖与机器独立代码优化
代码优化包括两种优化技术,即机器依赖和机器独立代码优化。机器依赖和机器独立代码优化之间的差异是将机器依赖性优化应用于对象代码,而机器独立代码优化应用于中间代码。
下载机器依赖与机器独立代码优化的PDF版本
您可以下载本文的PDF版本,并根据引文注释将其用于离线目的。请在此处下载PDF版本之间的区别机器依赖and Machine Independent Code Optimization
参考:
1。“Compiler Design | Code Optimization.” GeeksforGeeks.在这里可用
2.点,教程。“编译器设计 - 代码优化。”www.tutorialspoint.com,教程点,2017年8月15日。在这里可用
3.调查4你。“ Jntuh CSE研究材料。”机器依赖和独立代码优化之间的差异。在这里可用
图片提供:
1。’Compiler’ By I, Surachit,(CC BY-SA 3.0)通过下议院维基梅迪亚
Leave a Reply