views:

349

answers:

11

Do you normally set your compiler to optimize for maximum speed or smallest code size? or do you manually configure individual optimization settings? Why?

I notice most of the time people tend to just leave compiler optimization settings to their default state, which with visual c++ means max speed. I've always felt that the default settings had more to do with looking good on benchmarks, which tend to be small programs that will fit entirely within the L2 cache than what's best for overall performance, so I normally set it optimize for smallest size.

A: 

We always use maximize for optimal speed but then, all the code I write in C++ is somehow related to bioinformatics algorithms and speed is crucial while the code size is relatively small.

Konrad Rudolph
A: 

Memory is cheap now days :) So it can be meaningful to set compiler settings to max speed unless you work with embedded systems. Of course answer depends on concrete situation.

aku
+1  A: 

For me it depends on what platform I'm using. For some embedded platforms or when I worked on the Cell processor you have restraints such as a very small cache or minimal space provided for code.

I use GCC and tend to leave it on "-O2" which is the "safest" level of optimisation and favours speed over a minimal size.

I'd say it probably doesn't make a huge difference unless you are developing for a very high-performance application in which case you should probably be benchmarking the various options for your particular use-case.

Mike McQuaid
+4  A: 

As a Gentoo user I have tried quite a few optimizations on the complete OS and there have been endless discussions on the Gentoo forums about it. Some good flags for GCC can be found in the wiki.

In short, optimizing for size worked best on an old Pentium3 laptop with limited ram, but on my main desktop machine with a Core2Duo, -O2 gave better results over all.

There's also a small script if you are interested in the x86 (32 bit) specific flags that are the most optimized.

If you use gcc and really want to optimize a specific application, try ACOVEA. It runs a set of benchmarks, then recompile them with all possible combinations of compile flags. There's an example using Huffman encoding on the site (lower is better):

A relative graph of fitnesses:

   Acovea Best-of-the-Best: **************************************                (2.55366)
     Acovea Common Options: *******************************************           (2.86788)
                       -O1: **********************************************        (3.0752)
                       -O2: ***********************************************       (3.12343)
                       -O3: ***********************************************       (3.1277)
           -O3 -ffast-math: **************************************************    (3.31539)
                       -Os: *************************************************     (3.30573)

(Note that it found -Os to be the slowest on this Opteron system.)

Claes Mogren
+2  A: 

I prefer to use minimal size. Memory may be cheap, cache is not.

Leon Timmermans
And you check that produces faster code?
Tom Hawtin - tackline
+1  A: 

Microsoft ships all its C/C++ software optimized for size. After benchmarking they discovered that it actually gives better speed (due to cache locality).

On Freund
+1  A: 

Besides the fact that cache locality matters (as On Freund said), one other things Microsoft does is to profile their application and find out which code paths are executed during the first few seconds of startup. After that they feed this data back to the compiler and ask it to put the parts which are executed during startup close together. This results in faster startup time.

I do believe that this technique is available publicly in VS, but I'm not 100% sure.

Cd-MaN
+1  A: 

There are many types of optimization, maximum speed versus small code is just one. In this case, I'd choose maximum speed, as the executable will be just a bit bigger. On the other hand, you could optimize your application for a specific type of processor. In some cases this is a good idea (if you intend to run the program only on your station), but in this case it is probable that the program will not work on other architecture (eg: you compile your program to work on a Pentium 4 machine -> it will probably not work on a Pentium 3).

botismarius
+1  A: 

Build both, profile, choose which works better on specific project and hardware.

For performance critical code, that is - otherwise choose any and don't bother.

ima
A: 

This depends on the application of your program. When programming an application to control a fast industrial process, optimize for speed would make sense. When programming an application that only needs to react to a user's input, optimization for size could make sense. That is, if you are concerned about the size of your executable.

Mark D
A: 

Tweaking compiler settings like that is an optimization. On the principle that "premature optimization is the root of all evil," I don't bother with it until the program is near its final shipping state and I've discovered that it's not fast enough -- i.e. almost never.

Head Geek