L
L
LittleBuster2015-08-30 15:29:43
C++ / C#
LittleBuster, 2015-08-30 15:29:43

Gcc -Os or -O3?

Everywhere they write -O3 gives optimization to speed up the program, and -Os to reduce its size. I don’t understand one thing, is the size reduction ie. the number of assembler instructions should not speed up the program even more? For example, on a synthetic test, it was established that -O3 Y does not change the size of a c++ program, and -Os reduces it from 160 lines to 70. Shouldn't the program work faster because of this? But everywhere in the software we see -O2 and -O3.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2015-08-30
@LittleBuster

No, size and speed are opposite optimizations.
You can reduce the size of the program by eliminating inline calls, which will lead to brakes.
Another example: you can speed up the program by replacing loops with linear sets of instructions (loop-unroll (which includes -O3)), which again increases the size of the binary.

I
Ilya Popov, 2015-08-30
@encyclopedist

No, it's not. This would be the case if the commands ran in a linear fashion and took the same amount of time, but we have loops and functions, and not all commands take the same amount of time.
There are important optimizations that greatly increase the size of programs, but at the same time reduce the execution time. These are:
- function inlining, when the function code is inserted into the call site. In this case, the function body is duplicated several times in different places, but time is saved for calls (jump commands, packing-unpacking arguments)
- loop unrolling. Instead of looping through one element 1000 times, it's faster to duplicate the body of the loop 4 times, for example, and execute the resulting loop 250 times. Time is saved by reducing the number of conditional jumps.
- vectorization
- and some others

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question