Compilation and Optimization

Compilation and Optimization

What is Compilation ?

The compilation is a process of converting the source code into object code it is needed because computer understands only machine code (object code)

So there are 4 stages of compilation.

No alt text provided for this image

source files are by convention named with .c extension and the command “gcc” is used to compile C source files. GCC stands for GNU Compiler Collection and it is a compiler system produced by the GNU project.

There are four steps to the compilation process: preprocessing, compiling, assembly, and linking.

To understand these stages? let me take a small c program of hello world written in hello.c file which is a source code

No alt text provided for this image

Once we do compilation we get an executable code

No alt text provided for this image

But what if you want to check every compilation stages in between or before generating an executable format?

To get all stages of compilation use:-

No alt text provided for this image

Here? hello.c → source code

Hello.i → preprocessed expanded code

Hello.o → assembler code (object code) input to linker stage

Hello.s → compiler output assembly code

Through GCC we can stop at desired stages as well

Like for example

-E – is used for preprocessing stage

No alt text provided for this image

Similarly?

-S – Assembler

-c – Object code

Can be used!


Apart from these compilation stages we can also look into optimization of a program which plays a major role in memory allocation and speed factor of any application or software


In order to control compilation-time and compiler memory usage, and the trade-offs between speed and space for the resulting executable, GCC provides a range of general optimization levels, numbered from 0--3, as well as individual options for specific types of optimization.

An optimization level is chosen with the command line option -OLEVEL, where LEVEL is a number from 0 to 3. The effects of the different optimization levels are described below:

-O0 or no -O option (default)

At this optimization level GCC does not perform any optimization and compiles the source code in the most straightforward way possible. Each command in the source code is converted directly to the corresponding instructions in the executable file, without rearrangement. This is the best option to use when debugging a program and is the default if no optimization level option is specified.

-O1 or -O

This level turns on the most common forms of optimization that do not require any speed-space tradeoffs. With this option the resulting executables should be smaller and faster than with -O0. The more expensive optimizations, such as instruction scheduling, are not used at this level. Compiling with the option -O1 can often take less time than compiling with -O0, due to the reduced amounts of data that need to be processed after simple optimizations.

-O2

This option turns on further optimizations, in addition to those used by -O1. These additional optimizations include instruction scheduling. Only optimizations that do not require any speed-space tradeoffs are used, so the executable should not increase in size. The compiler will take longer to compile programs and require more memory than with -O1. This option is generally the best choice for deployment of a program, because it provides maximum optimization without increasing the executable size. It is the default optimization level for releases of GNU packages.

-O3

This option turns on more expensive optimizations, such as function inlining, in addition to all the optimizations of the lower levels -O2 and -O1. The -O3 optimization level may increase the speed of the resulting executable, but can also increase its size. Under some circumstances where these optimizations are not favorable, this option might actually make a program slower.

-funroll-loops

This option turns on loop-unrolling, and is independent of the other optimization options. It will increase the size of an executable. Whether or not this option produces a beneficial result has to be examined on a case-by-case basis.

-Os

This option selects optimizations which reduce the size of an executable. The aim of this option is to produce the smallest possible executable, for systems constrained by memory or disk space. In some cases a smaller executable will also run faster, due to better cache usage.

Here i am using a simple sort program to print time taken in sorting 30000 elements you can see using various optimization levels have reduced the time

No alt text provided for this image

要查看或添加评论,请登录

Nikita Yadav的更多文章

  • BLE ARCHITECTURE (HOST )

    BLE ARCHITECTURE (HOST )

    HCI Host Controller Interface Interface between host and the controller Responsible or to main functions : - sending…

    2 条评论
  • BLE ARCHITECTURE (CONTROLLER)

    BLE ARCHITECTURE (CONTROLLER)

    THE BLE ARCHITECTURE Three main block of BLE architecture Application : The application uses the host software to build…

    2 条评论
  • BLUETOOTH INTRODUCTION

    BLUETOOTH INTRODUCTION

    What is bluetooth ? A short range wireless technology focused on low power and low-bandwidth applications Each…

  • WiFi Interfaces

    WiFi Interfaces

    We know that in past few years transition from wired networks to wireless networks is very much evident and fast…

    2 条评论
  • Embedded Basics-1

    Embedded Basics-1

    Difference between API's and HAL Microcontrollers have reached a cost point and capability stand point that developers…

社区洞察

其他会员也浏览了