The GNU C++ compiler (like other Linux C++ compilers) offers various
levels of optimization from none (-O0
) through to
(-O3
). There is a trade off to be made—more
optimization means longer compile times, but faster run times.
GNU C++ also offers -Os
, to optimize for
space. This is equivalent to -O2
, but omitting any
optimizations that tend to increase the size of the program.
Table 7.3 shows the impact of the different optimization levels on the example design.
Run Description |
Build Time |
Run Time |
Performance |
---|---|---|---|
|
13.98 s |
25.05 s |
47.10 kHz |
|
21.51 s |
13.13 s |
89.90 kHz |
|
32.77 s |
12.76 s |
92.46 kHz |
|
35.35 s |
12.39 s |
95.25 kHz |
|
26.23 s |
12.24 s |
96.41 kHz |
Table 7.3. Comparison of model performance with different compiler optimization settings.
Almost all the benefit is gained from -O1
, but
there are incremental benefits, at the expense of greater compile
times for higher levels of optimization.
Note however that the highest performance is with
-Os
. Code generated by Verilator (and its
commercial rivals) has a classic "cache-busting" structure. On each
code cycle execution starts at the top and proceeds linearly to the
bottom. Anything that reduces the code size, increases the
likelihood of code remaining in the cache, and so can have a very
large performance benefit.
The recommendation is to use -Os
as the preferred
C++ compiler option.