How do you usually customize your compiler optimization settings?

Do you usually tune your compiler to optimize for maximum speed or smallest code size? or do you manually configure individual optimization parameters? Why?

I notice that most of the time people tend to just leave the compiler optimization settings to their default state, which with visual C ++ means maximum speed. I always felt that the default settings have more in common with good test results, which are usually small programs that will fit completely into the second level cache memory than what is best suited for overall performance, so I I usually set it to the minimum size.

+6
c ++ optimization c compiler-optimization
source share
11 answers

As a Gentoo user, I tried quite a few optimizations for the entire OS, and the Gentoo forums had an endless discussion about this. Some good flags for GCC can be found on the wiki .

In short, optimizing for size worked best on an old Pentium3 laptop with a limited bar, but on my main desktop machine with Core2Duo -O2 it gave the best results for everyone.

Also, a small script if you are interested in the most important x86 (32-bit) flags, the most optimized ones.

If you use gcc and really want to optimize a specific application, try ACOVEA . It runs a test suite, and then recompiles them with all possible combinations of compilation flags. Here is an example of using the Huffman encoding on the site (below 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 he found that -O is the slowest in this Opteron system.)

+6
source share

I prefer to use the minimum size. Memory can be cheap, not cache.

+2
source share

Besides the fact that there is a cache (as Freund said), another thing that Microsoft does is to profile your application and find out which code paths are executed during the first few seconds of launch. After that, they pass this data back to the compiler and ask him to put the parts that are executed at startup time together. This leads to faster startup times.

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

+2
source share

For me, it depends on which platform I use. For some embedded platforms, or when I was working on a Cell processor, you have limitations, such as a very small cache or minimal code space.

I use GCC and usually leave it at "-O2", which is the "safest" level of optimization and contributes to speed with a minimum size.

I would say that this probably doesn’t matter much if you are not developing an application with very high efficiency, in which case you probably should compare the different options for your specific use case.

+1
source share

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

+1
source share

There are many types of optimization, the maximum speed compared to a small code is one. In this case, I would choose the maximum speed, since the executable file will be slightly larger. On the other hand, you can optimize your application for a certain 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 likely that the program will not work in a different architecture (for example: you are compiling your program to work on Pentium 4 - this is probably not will work on Pentium 3).

+1
source share

Create both a profile, choose which works best on a specific project and equipment.

For critical performance code, that is - otherwise, select any and don’t worry.

+1
source share

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

0
source share

Memory is cheap now :) So it might make sense to set compiler options to maximum speed if you are not working with embedded systems. Of course, the answer depends on the specific situation.

0
source share

It depends on the application of your program. When programming an application to control a fast production process, speed optimization will make sense. When programming an application that only needs to respond to user input, optimization for size may make sense. That is, if you are concerned about the size of your executable file.

0
source share

Setting compiler options like this is an optimization. By the principle that “premature optimization is the root of all evil”, I don’t worry about it until the program comes close to its final state of shipment, and I found that it is not fast enough - i.e. Almost never.

0
source share

All Articles