1 |
Ben de Groot wrote: |
2 |
> |
3 |
> -Os optimizes for size, while -O2 optimizes for speed. There is no need |
4 |
> at all to use -Os on a modern desktop machine, and it will run |
5 |
> comparatively slower than -O2 optimized code, which is probably not what |
6 |
> you want. |
7 |
> |
8 |
|
9 |
There are a couple of schools of thought on that, and I think |
10 |
performance can depend a great deal on what program you're talking about. |
11 |
|
12 |
On any machine, memory is a limited resource. Oh sure, you could just |
13 |
"spend a little more on decent RAM", but you could also "spend a little |
14 |
more on a decent CPU" or whatever. For a given amount of money you can |
15 |
only buy so much hardware, so any dollar spent on RAM is a dollar not |
16 |
spent on something else. |
17 |
|
18 |
So, if you reduce the memory footprint of processes, then you increase |
19 |
the amount of memory available for buffers/cache. That cache is many |
20 |
orders of magnitude faster than even the fastest hard drive. You also |
21 |
reduce swapping which obviously greatly slows things down. |
22 |
|
23 |
On the other hand, if you have a smaller program that does |
24 |
highly-CPU-intensive tasks like compression/transcoding/etc then speed |
25 |
optimization makes sense just about all the time (generally -O2 only - |
26 |
-O3 sometimes does worse due to L2 cache misses). |
27 |
|
28 |
So, there are trade-offs. To make things even more complicated the |
29 |
practical results can be impacted by your CPU model - different CPUs |
30 |
have a different cost for looping/jumping/branching vs cache misses. |
31 |
And the compiler makes a big difference - many of these observations |
32 |
date back to the gcc 3.4 days. I've heard that newer GCC versions are |
33 |
more aggressive with size optimization at the expense of speed, which |
34 |
could tip the balance. |
35 |
|
36 |
A while back there were some posts from folks who had done some actual |
37 |
benchmarking. I don't think it has been repeated. Note that I wouldn't |
38 |
expect -Os to have much benefit unless you compile EVERYTHING on the |
39 |
system that way - since the freeing up of memory is cumulative. The |
40 |
rather high level of pain associated with rebuilding your system with |
41 |
-Os vs -O2 for some benchmarking and subjective evaluation is probably |
42 |
the reason why everybody has an opinion but there isn't much hard data. |
43 |
|
44 |
Right now I'm using -Os across the board, and -O2 on an exception basis. |
45 |
Maybe I should give that some more thought... |