7

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).

  • 2
    Possible duplicate of [GCC: Difference between -O3 and -Os](https://stackoverflow.com/questions/19689014/gcc-difference-between-o3-and-os) –  Nov 22 '17 at 16:59
  • I should add that while the question I've flagged doesn't specifically ask about -O2, it is answered by the accepted answer there. –  Nov 22 '17 at 17:00
  • 2
    Your question title refers to compilation time, but your question text seems more concerned about execution time, which isn't the same thing. – lurker Nov 22 '17 at 17:03
  • Yes, you are right. I changed it. Thanks. – Monstermania Nov 22 '17 at 17:06

2 Answers2

2

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

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.

VladP
  • 529
  • 3
  • 15
  • Thanks! I managed to pass only -O0, where my assembly code runs in 57s and -O1-3 in about 33s. It is that good? – Monstermania Nov 22 '17 at 17:02
  • Yes, it is that good, and for further optimization, you probably need to spend a lot of time and effort. C++ code is typically more difficult to optimize in comparison to C code. If you can, you may try to play with memory optimization by changing the size of stack and heap. Sometimes, it helps. – VladP Nov 22 '17 at 17:06