Skip to content

Code Optimization

December 4, 2013

This article is the subsequent article about embedded system code/RAM optimization. Please refer HERE for first article.

In this article I will discuss in more detail each of the techniques that I used to perform code optimization.

1. Optimize Switch
All compiler comes with optimization option that enable certain form of optimization, usually on code or size optimization. This is the easiest way to perform code optimization. Several optimization level is available to select, from the least compact code to most compact code. It is also possible to perform optimization on individual function, single source file or all the source file.

2. Release vs Debug
Some compiler will have 2 different profile setup for project created. In Debug mode, usually more code size is use to assist on debugging. Once the debugging process is complete, user can switch to Release mode in order to further reduce the program code size.

3. Remove Un-used Function/Features
By identifying un-used function/features, we can then remove/exclude certain function to achieve code reduction. The down side of this it is very time consuming and engineer have to understand which code should be remove and shouldn’t.
As for non-vector function that is not being called by function (un-called function), usually this will be remove by compiler during optimization stage.

4. Prefer Internal RAM Over External RAM – Architecture Dependant
Some MCU will have small size of internal RAM(1/2 KBytes) and a larger external RAM(tenths of KBytes). Accessing data from internal RAM is faster(shorter machine code) as compare to external RAM. By declaring global variable in internal RAM(using compiler keyword ‘near/idata’) will have some code size. This amount will be added up to significant value if the variable is being access many times in the source code.

Using similar concept, usage of local variable can achieve the same result. Within a function, if a global variable is require to be use (for its value read or comparison), then the value can be assign to local variable, and access by using local variable.

5. Combine function/Pattern Searching
Search for function that has been call mostly, with the same parameter pass into the particular function.
For e.g.

f1(a,b,c,d); //call many times in source, with same parameter of a,b,c,d
// change to
f1_replacment()
{

f1(a,b,c,d);

}

The effect of reduction depends on 1) how many call is being perform in source 2)how many parameter is require for function call, the more of it, the more reduction is possible.
The trade off this is additional jump of function call.

6. Variable declaration consideration
Using system architecture native type whenever possible. Example, in 8-bit controller environment is it better to use 8-bit type variable. While for 32-bit controller, 32-bit type buffer is preferred.

Advertisements

From → Embedded System

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: