Code Optimization

Dear readers, please note that this is the old website of maxEmbedded. The articles are now no longer supported, updated and maintained. Please visit the new website here and search for this post. Alternatively, you can remove .wordpress from the address bar to reach the new location.

Example: If the website address is, then removing .wordpress from it will become

We apologize for the inconvenience. We just want to give you a better viewing and learning experience! Thanks!

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! 😀

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! 🙂


8 responses to “Code Optimization

  1. Pingback: Using AVR Studio 5 « maxEmbedded·

  2. Pingback: Using Atmel Studio 6 « maxEmbedded·

  3. 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.

      • 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.

  4. Pingback: I/O Port Operations in MSP430 | maxEmbedded·

We'd love to hear from you!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s