compiler code optimization

Thread Starter

anukalp

Joined Jul 28, 2018
141
I am struggling to understand meaning of compiler code optimization. Compiler code Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed.

I do not understand what does compiler code optimization mean, can someone explain what is meaning of compiler code optimization?
 

peterdeco

Joined Oct 8, 2019
27
OK Anukalp. I'll give it a try. Say you're programming a microcontroller to check if a switch is closed (computer programming similar). You have 1 second to switch it to light an LED or it will not do it. Here is sloppy programming in BASIC:
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
if porta.1 = 1 then high portb.1 'switch closed light LED
pause 100 'wait 1/10 second
goto off

A more efficient way would be this:
begin:
if porta.1 = 1 then high portb.1
pause 100
let X=X+1
if X=10 then goto off 'scanned state of porta.1 10 times
goto begin

As you can see, that whole bunch of code is drastically reduced and will do the same thing. Hope this helps.
 
Last edited:

MrSoftware

Joined Oct 29, 2013
1,576
When writing code in a higher level language, such as C or C++, the code must be converted into machine code that the processor can understand. This is done by the compiler. During this conversion the compiler can do things differently depending on the goal. For example, say you make a loop to print a line 10 times:

Code:
for(int i = 0; i < 10; i++)
    printf("Line number: %i\n", i);
Now assume you enable the compiler optimization for maximum speed. The code might execute faster if the compiler just duplicates the printf code 10x instead of making a real loop. If you change the optimization for code size, then a loop will definitely take less memory than 10 individual sets of code to print a line so it may leave the loop. This is not the best example, but hopefully you get the idea. Here is a screen shot of optimization settings from Microsoft Visual Studio just for an example, other compilers will be similar, some have multiple levels of optimization:

1574012186909.png
 

Papabravo

Joined Feb 24, 2006
12,576
A possibly more concrete example is a simple loop with an expression inside that always computes the same value. A smart compiler can move the expression evaluation outside the loop and make a dramatic improvement in execution time.

Another example is the evaluation of common sub-expressions. A smart compiler will evaluate the sub-expression only once and then use a temporary variable to hold that value for all occurrences of the sub-expression.
 
Last edited:

nsaspook

Joined Aug 27, 2009
6,510
I am struggling to understand meaning of compiler code optimization. Compiler code Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed.

I do not understand what does compiler code optimization mean, can someone explain what is meaning of compiler code optimization?
'improve the code' sometimes means convert your nice structured programming styled code into messy goto filled ASM that would give Dijkstra a massive headache.

https://www.embedded.com/advanced-compiler-optimization-techniques/
Complex Branch Optimization
With code reuse becoming a critical factor in delivering products to market faster, writing well-structured readable code is very important. In some cases structured programming techniques lead to code that is less efficient. Compilers, of course, do not have to worry about “code reviews” or program maintenance and have no qualms about using constructs such as unstructured jumps (“goto”). This enables a sophisticated optimizer to “reimplement” the program logic for greater performance.


A good example of an optimization that reimplements structured programs for higher performance is the complex branch optimization. This optimization rewrites code containing branches and fall-throughs to conditional branches where the outcome can be compared. A code fragment of a loop with multiple exits demonstrates how this optimization works:
...
The optimizer detects that the loop will always be entered and that stmt() will always be executed. The compiler recognizes that stmt2() will never be executed if fl is false and if e1 is true, fl will always be false. This enables it to produce a much more efficient implementation of the same code. However, it is not recommended for programmers to follow this coding style themselves!
 
Last edited:

MrSoftware

Joined Oct 29, 2013
1,576
To add to what @nsaspook said, if you ever try stepping through code in your debugger and it's jumping all over the place seemingly without logic, check your compiler optimization settings. Debugging through optimized code will give you more than a few new gray hairs.
 
Top