Pages Menu
Categories Menu

Posted by on Jun 27, 2012 in Microcontrollers | 7 comments

Code Optimization

Code Optimization

In this post, you get to know what optimization is, why do we need it, its causes, effects and an example.

Optimization. Many places we have come across it. Here in this short post, I will give the readers an idea of code optimization, what is it and why is it necessary. However I won’t go into the details as to how it is done and all.

What is it?!

Code optimization is a technique used by compilers in order to modify the program more efficient by utilizing fewer resources, or making it execute fastly, reduce compilation/building time, reduce size, drawing lesser power, effective memory usage, etc. But all this happens without changing the output or its side effects.

Talking in terms of embedded electronics, we use this built-in technique of the compiler in order to generate efficient hex/machine level files. Let’s take a small example.

Visit this post and do as directed. Create a new project, type in the same program, configure your project properly. Now during code optimization, choose the optimization level as -O0. Then build the project and view the hex file as directed in that post. You will see something like this:


Haha, don’t worry, I won’t ask you to decipher it! Now change the optimization level to O2, build the project and then view the hex file again. What do you see?


Hey, don’t be shocked! This is what optimization is! Question answered! :D

Why is it necessary?

Well, I would like to put forth the above question as What if I don’t optimize my code? 

Ideally speaking, microcontrollers and processors are supposed to be programmed in assembly language, which seems like Greek to most people. So we usually program in some higher level language like C, C++, python, JAVA, etc. and leave the rest of the headache to the compiler which compiles and builds our code. Now it all depends upon the compiler how it converts the code and builds it.

Now there lies the problem. The compiler is nothing but a software with some rules. So many a times, during compilation, the outcome is very inefficient. This is why modern compilers come with several options for code optimization. The outcome is usually a better and efficient file. The newer file may require fewer resources (like it utilizes lesser RAM), may require lesser program program memory, may reduce the execution time, etc. An example is shown above where after optimization, the size of the hex file is drastically reduced!

But how come same code results in more optimized code?

But how come the same high level code result in a more optimized low level code? Well, there may be many reasons, there is no particular answer to this. It might have compressed your code, or removed the comments, or removed the repeated portion of the code, or removed any variables that you may not have used, changed your code structure to result in an equivalent one with lesser clock cycles, tweaked the assembly a little, etc. As embedded developers, we need not go into these details and have faith in the compiler we use to generate the most optimum code. But as programmers, you should have some idea as to how you can tweak the assembly code and learn a bit about optimization.

I suggest you all to take some time and read the following tutorial at AVR Freaks. This would give you a clear understanding of Optimization and the importance of volatile in GCC.

Please comment below if you like to share some ideas. I will be glad to see them. For more updates, you can subscribe to this site, or grab the RSS Feeds! See you around! :)


  1. I think a embedded developer we need to know what compiler exactly does. Because many times some desired functionality is affected because of compiler optimization. Because of compiler optimization, many times we need to declare some variables as ‘volatile’ so that compiler knows that has preserve it and not it optimize it away.
    So if we know what kind of optimization techniques compiler uses, we can take care of that in our code.

    • Yeah exactly! And that’s why details are provided for the users. Like for example, for AVR, the information is available in the GCC-GNU User Manual.

  2. hello I am not finding optimize option in project atmel studio.

    • Hi ToorBabbu,
      Please refer to this tutorial, and scroll down. There is a section on Code Optimization.

      • Hi Mayank firstly Thank u gentelman for reply.I hava solved succ….Mayank have u free time any of week days? I would like chatting on Avr programmign.You have compliments, according to me you website is enough beneficial for newbies also is designed in good manner also colors are so cool for reading.
        once again thanks for reply.

  3. Hi Mayank,

    What if i select leve o3 or the last one for optimization ? will it have any adverse effect on operation ? why you prefered O2 level optimization ?

    • Good question! And I’m surprised that this question hasn’t been asked yet!
      These are called compiler optimization levels. Even if you select level o3, your code would still work. It all depends upon how the compiler creates a disassembly of your code. I chose o2 level because I felt that a moderate level of optimization would be perfect. If you optimize it too much, some more portion of your code would be stripped off from the disassembly.


  1. Using AVR Studio 5 « maxEmbedded - [...] know what optimization is, view this. For more details regarding optimization of code in AVR, visit this page. For…
  2. Using Atmel Studio 6 « maxEmbedded - [...] know what optimization is, view this. For more details regarding optimization of code in AVR, visit this page. For…
  3. I/O Port Operations in MSP430 | maxEmbedded - […] be uint8_t (why not int8_t?). But these days, compilers have also become smart. They know how to optimize your…

Leave a Reply

%d bloggers like this: