Pages

Optimization of Compilation


There are some optimization options of compilation that we can use.
1.       –O1
2.       –O2
3.       –O3, optimization in speed and being hoped to have small size too.
4.       –Os, optimization in size.
Ok, let we see by an example to see the difference among them.
1.       Make a code2.c
int main()
{
int a=2;
int b=3;
int c=4;
int sum (int x, int y, int z)
{
int t = x+y+z;
return t;
}
sum(a,b,c);
return 0;
}
2.       Using command prompt to compile and produce an object file. Add -Ox before -c. For example, $ gcc -O2 -c code2.c. (See my posting about how to compile program using GCC in command prompt)
3.       Disassemble the object file in command prompt. (See my posting about how to disassemble program)
4.       See the result on picture below.
compilation using O1 optimization option


compilation using O2 optimization option


compilation using O3 optimization option


compilation using Os optimization option
Now you can see the differance of compilation result with different optimization option. The optimization will affect the speed and size of the program.

http://electricalgaze.com

No comments:

Post a Comment