https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91852
Bug ID: 91852 Summary: Compile the code with -O0 is slower than with -O1/-O2/-O3 Product: gcc Version: 8.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: hehaochen at hotmail dot com Target Milestone: --- Compile the following code with -O0 is slower than with -O1/-O2/-O3 : -O1 applies some optimization on execution time and binary size, I expected using -O1 takes more time than -O0. So is there something wrong with -O0 in this case? ------------------------------------------------ class c { public: c() {} }; class a { c array[500000] {}; }; int main() { a t; } ------------------------------------------------ root@72c6552d0d56:/# gcc --version gcc (GCC) 8.3.0 Copyright (C) 2018 Free Software Foundation, Inc. root@72c6552d0d56:/downloads# time gcc -std=c++14 -O0 -Wall a.cc real 1m14.121s #### SLOW #### user 1m7.966s sys 0m5.948s root@72c6552d0d56:/downloads# time gcc -std=c++14 -O1 -Wall a.cc real 0m26.268s user 0m23.591s sys 0m2.657s root@72c6552d0d56:/downloads# time gcc -std=c++14 -O2 -Wall a.cc real 0m26.413s user 0m23.704s sys 0m2.708s root@72c6552d0d56:/downloads# time gcc -std=c++14 -O3 -Wall a.cc real 0m26.553s user 0m23.874s sys 0m2.678s The result is nearly same in gcc-9.2.0 gcc-7.3.0 gcc-6.5.0 The root cause lies in "phase opt and generate" in -ftime-report The problem falls on '-O1' in older version: (gcc-4.8.5 gcc-5.4.0) ------------------------------------------------ class c { public: c() {} }; class a { // As described in bug 77443: // compile time grows super-linearly in (gcc-4.8.5 gcc-5.4.0) c array[30000] {}; }; int main() { a t; } ------------------------------------------------ # time gcc -O0 -std=c++11 -Wall a.cc 1.25s user 0.22s system 99% cpu 1.464 total # time gcc -O1 -std=c++11 -Wall a.cc 7.93s user 0.16s system 99% cpu 8.089 total # time gcc -O2 -std=c++11 -Wall a.cc 7.94s user 0.15s system 99% cpu 8.097 total # time gcc -O3 -std=c++11 -Wall a.cc 7.92s user 0.18s system 99% cpu 8.095 total In gcc-5.4.0 -O0: 0m1.834s -O1: 0m5.467s -O2: 0m5.407s