Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

optimization mode
optimization

How can optimizations be controlled with compiler options?

Arslan Tariq

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Optimization

Optimization is a technique of modifying code to improve code quality and efficiency. The code is optimized to be smaller, use less memory, run faster, and perform fewer input or output operations.

Options that control optimization

The compiler performs optimizations based on what it knows about the program. Compiling multiple files at once into a single output file allows the compiler to use the information obtained from all files when compiling each file. The compiler attempts to improve performance and code size at the expense of compile time and possibly the ability to debug the program. Most optimizations are completely disabled at -O0 or when the -O level is not set on the command line. Also, -Og suppresses many optimization passes.

Control optimization

Depending on the purpose and how GCC is configured, slightly different sets of optimizations than those listed here can be enabled at each -O level.

-O

The compiler execution time without performing compile-time-intensive optimizations and tries to reduce code size with -O.

-O1

Optimizing compilation takes more time, and large functions require extra memory.

-O2

GCC performs almost all supported optimizations without a trade-off between space and speed. Compared to -O, this option increases compile-time and the performance of the generated code.

-O3

Compared to -O2, the compiler tries to optimize further in this mode.

-O0

Reduces compilation time and ensures that debugging produces expected results. -O0 is the default setting.

-Os

The compiler tries to optimize for size.

-Og

A reasonable optimization level, while maintaining fast compilation and a good debugging experience, -Og should be the best optimization level for standard edit and compile-debug cycles. It is more suitable for generating debug code than -O0 because some compiler channels that collect debug information at -O0 are disabled.

-Oz

Aggressively optimize for size rather than speed. If these instructions require fewer bytes to encode, we can increase the number of instructions. -Oz behaves like -Os, including enabling most -O2 optimizations.

RELATED TAGS

optimization mode
optimization

CONTRIBUTOR

Arslan Tariq
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring