# Frequency Reduction in Code Optimization

• Last Updated : 21 Nov, 2019

Prerequisite – Compiler Design | Code Optimization
Frequency reduction is a type in loop optimization process which is machine independent. In frequency reduction code inside a loop is optimized to improve the running time of program. Frequency reduction is used to decrease the amount of code in a loop. A statement or expression, which can be moved outside the loop body without affecting the semantics of the program, is moved outside the loop. Frequency Reduction is also called Code Motion.

Objective of Frequency Reduction:
The objective of frequency reduction is:

• To reduce the evaluation frequency of expression.
• To bring loop invariant statements out of the loop.

Below is the example of Frequency Reduction:

Program 1:

 `// This program does not uses frequency reduction.``#include `` ` `using` `namespace` `std;`` ` `int` `main()``{``    ``int` `a = 2, b = 3, c, i = 0;`` ` `    ``while` `(i < 5) {``        ``// c is calculated 5 times``        ``c = ``pow``(a, b) + ``pow``(b, a); `` ` `        ``// print the value of c 5 times``        ``cout << c << endl; ``        ``i++;``    ``}``    ``return` `0;``}`

Program 2:

 `// This program uses frequency reduction.``#include `` ` `using` `namespace` `std;`` ` `int` `main()``{``    ``int` `a = 2, b = 3, c, i = 0;`` ` `    ``// c is calculated outside the loop``    ``c = ``pow``(a, b) + ``pow``(b, a); `` ` `    ``while` `(i < 5) {`` ` `        ``// print the value of c 5 times``        ``cout << c << endl; ``        ``i++;``    ``}``    ``return` `0;``}`

Output:

```17
17
17
17
17```

Explanation:
Program 2 is more efficient than Program 1 as in Program 1 the value of c is calculated each time the while loop is executed. Hence the value of c is calculated outside the loop only once and it reduces the amount of code in the loop.

My Personal Notes arrow_drop_up