Arya
Arya

Reputation: 371

Optimization level in GCC for reducing executable size

I am learning optimization by gcc. From Optimization levels,it is given that in level Os , gcc specifically select optimization required to reduce size of executable. I wrote simple program.

#include<stdio.h>

int main()
{
    const int k=7;
    printf("k = %d\n",k);
    return 0;
}

And tested for all levels. It gave me size as follows

O0 - 4883
O1 - 4883
O2 - 4867
O3 -4867
Os - 4879

It should give least size in level Os . But it did not do so. Can anyone please tell me if I am wrong. Thanks

Upvotes: 0

Views: 1432

Answers (1)

MattP
MattP

Reputation: 2863

Think of the Optimization Levels as a guideline style for the compiler, they will never guarantee the smallest or fastest executable but will build the code in a way best suited the the level of optimization you are aiming for.

0s will not test all levels then pick the smallest executable, it will just build using rules that tend towards smaller executable sizes rather than faster execution for example.

Add a few loops to your program so it has more options for the compiler then you might start to see some more significant differences. If you really want to understand the differences in Optimization Levels then start disassembling the executable and see what assembly code the compiler produced and compare the various styles.

Upvotes: 6

Related Questions