Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimized Execution Time

Because of a school assignment I have to convert a C++ code to assembly(ARMv8). Then I have to compile the C++ code using GCC's -O0,-O1,-O2,-O3 and -Os optimizations, write down the time and compare with the execute time of my assembly code. As, I think I know -O3 have to be faster than -O1 and -O2. However, I get that -O2 is the fastest, then are -O1,-O3,-Os,-O0. Is that usual? (Calculated times are about 30 seconds).

like image 338
Monstermania Avatar asked Nov 22 '17 16:11

Monstermania


2 Answers

Notice that GCC has many other optimization flags.

There is no guarantee that -O3 gives faster code than -O2; a compiler can apply more optimization passes, but they are all heuristics and might be unsuccessful (or even slow down slightly your particular code). Hence it does happen that -O3 gives some slightly slower code than -O2 (on some particular input source code).

You could try a more recent version of GCC (the latest -in November 2017- is GCC 7, GCC 8 will go out in few months). You could also try some better -march= or -mtune= option.

At last, with your GCC plugin, you might add your own optimization pass, or change the order (and the set) of applied optimization passes (there are several hundreds different optimization passes in GCC). But you'll need a lot of work (perhaps a year or two) to be able to extend GCC.

You could tune optimization parameters, and some project (MILEPOST) has even used machine learning techniques to improve them.

See also slides and references on my (old) GCC MELT documentation.

like image 196
Basile Starynkevitch Avatar answered Sep 26 '22 19:09

Basile Starynkevitch


Yes, it is usual. Take the -Ox optimization as guide-lines. In average, they produce optimization that is advertise, but a lot depends on the style in which the code is written, memory layout, as well as the compiler itself. Sometimes, you need to try and fail many times before getting the optimal code. -O2 indeed gives the best optimization in most of the cases.

like image 44
VladP Avatar answered Sep 25 '22 19:09

VladP