I want to find out what code causes slow compilation times in gcc. I previously had a code being compiled slowly and someone told me the command-line switch that makes gcc to print each step that it compiles, including each function/variable/symbol and so on. That helped a lot (I could literally see in console where gcc chokes), but I forgot what was the switch.
-
Note: for general sluggishness, it can also be worth to `ptrace` the invocation (for example, a long list of `-I` directives might cause excessively long look up of header files). – Matthieu M. Dec 14 '12 at 13:21
-
Yes, thanks. But for this case I need to measure only preprocessed compilation (because distcc is used along with precompiled headers). – queen3 Dec 14 '12 at 13:23
3 Answers
I found it (from the gcc man page):
-Q
Makes the compiler print out each function name as it is compiled, and print some statistics about each pass when it finishes.
See also this answer to a quite similar question.
You very probably want to invoke GCC with -time
or more probably -ftime-report
which gives you the time spent by cc1
or cc1plus
... (the compiler proper started by the gcc
or g++
command) which shows the time spent in each internal phases or passes of the GCC compiler. Don't forget also optimizations, debugging, and warnings flags (e.g. -Wall -O -g
); they all slow down the compilation.
You'll learn that for C programs, parsing is a small fraction of the compilation time, as soon as you ask for some optimization, e.g. -O1
or -O2
. (This is less true for C++, when parsing can take half of the time, notably since template expansion is considered as parsing).
Empirically, what slows down GCC are very long function bodies. Better have 50 functions of 1000 lines each than one single function of 50000 lines (and this could happen in programs generating some of their C++ code, e.g. RefPerSys or perhaps -in spring 2021- Bismon).

- 223,805
- 18
- 296
- 547
-
In our c++ code, parsing is 50-60% of time (as reported by -ftime-report). – queen3 Dec 14 '12 at 13:59
-
Try the -v (verbose) compilation.
See this link:
http://www.network-theory.co.uk/docs/gccintro/gccintro_75.html
edit:
I understand. Maybe this will help:
gcc -fdump-tree-all -fdump-rtl-all
and the like (-fdump-passes). See here: http://fizz.phys.dal.ca/~jordan/gcc-4.0.1/gcc/Debugging-Options.html

- 4,446
- 36
- 61
-
No, it only prints each program that is invoked, not each symbol that it parses. – queen3 Dec 14 '12 at 13:12