Compiler Design and Construction - Old Questions

7.  What is the theme of code optimization? Why is code optimization important in computer?

6 marks | Asked in 2073

Code optimization is used to improve the intermediate code so that the output of the program could run faster and takes less space. It removes the unnecessary lines of the code and arranges the sequence of statements in order to speed up the program execution without wasting resources. It tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed. For e.g.

Intermediate code

Optimized Intermediate Code

t1 = 3.0;

t2 = id3 * t1;

t3 = id2 + t2;

id1 = t3;

t2 = id3 * 3.0;

id1 = id2 + t2;

A code optimizing process must follow the three rules given below:

  • The output code must not, in any way, change the meaning of the program.
  • Optimization should increase the speed of the program and if possible, the program should demand less number of resources.
  • Optimization should itself be fast and should not delay the overall compiling process.

Code optimization is important because:

  • Code optimization enhances the portability of the compiler to the target processor.
  • Code optimization allows consumption of fewer resources (i.e. CPU, Memory).
  • Optimized code has faster execution speed.
  • Optimized code utilizes the memory efficiently.
  • Optimized code gives better performance.
  • Optimized code often promotes re-usability.