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

<p>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.</p>

[{'answer_id': 53819, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 53823, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 53825, 'author': 'Mike McQuaid', 'author_id': 5355, 'author_profile': 'https://Stackoverflow.com/users/5355', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>I use GCC and tend to leave it on "-O2" which is the "safest" level of optimisation and favours speed over a minimal size.</p>\n\n<p>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.</p>\n'}, {'answer_id': 53826, 'author': 'Claes Mogren', 'author_id': 4992, 'author_profile': 'https://Stackoverflow.com/users/4992', 'pm_score': 4, 'selected': True, 'text': '<p>As a Gentoo user I have tried quite a few optimizations on the complete OS and there have been endless discussions on the <a href="http://forums.gentoo.org/" rel="noreferrer">Gentoo forums</a> about it. Some good flags for GCC can be found in the <a href="http://gentoo-wiki.com/Safe_Cflags" rel="noreferrer">wiki</a>.</p>\n\n<p>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.</p>\n\n<p>There\'s also a <a href="http://www.pixelbeat.org/scripts/gcccpuopt" rel="noreferrer">small script</a> if you are interested in the x86 (32 bit) specific flags that are the most optimized.</p>\n\n<p>If you use gcc and really want to optimize a specific application, try <a href="http://www.coyotegulch.com/products/acovea/" rel="noreferrer">ACOVEA</a>. 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):</p>\n\n<pre><code>A relative graph of fitnesses:\n\n   Acovea Best-of-the-Best: **************************************                (2.55366)\n     Acovea Common Options: *******************************************           (2.86788)\n                       -O1: **********************************************        (3.0752)\n                       -O2: ***********************************************       (3.12343)\n                       -O3: ***********************************************       (3.1277)\n           -O3 -ffast-math: **************************************************    (3.31539)\n                       -Os: *************************************************     (3.30573)\n</code></pre>\n\n<p>(Note that it found -Os to be the slowest on this Opteron system.)</p>\n'}, {'answer_id': 53833, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 2, 'selected': False, 'text': '<p>I prefer to use minimal size. Memory may be cheap, <b>cache is not</b>.</p>\n'}, {'answer_id': 53871, 'author': 'On Freund', 'author_id': 2150, 'author_profile': 'https://Stackoverflow.com/users/2150', 'pm_score': 1, 'selected': False, 'text': '<p>Microsoft ships all its C/C++ software optimized for size. After benchmarking they discovered that it actually gives better speed (due to cache locality).</p>\n'}, {'answer_id': 54181, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>I do believe that this technique is available publicly in VS, but I'm not 100% sure.</p>\n"}, {'answer_id': 57005, 'author': 'botismarius', 'author_id': 4528, 'author_profile': 'https://Stackoverflow.com/users/4528', 'pm_score': 1, 'selected': False, 'text': "<p>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.\nOn 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).</p>\n"}, {'answer_id': 61945, 'author': 'ima', 'author_id': 5733, 'author_profile': 'https://Stackoverflow.com/users/5733', 'pm_score': 1, 'selected': False, 'text': "<p>Build both, profile, choose which works better on specific project and hardware.</p>\n\n<p>For performance critical code, that is - otherwise choose any and don't bother.</p>\n"}, {'answer_id': 63195, 'author': 'Mark D', 'author_id': 7452, 'author_profile': 'https://Stackoverflow.com/users/7452', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 238097, 'author': 'Head Geek', 'author_id': 12193, 'author_profile': 'https://Stackoverflow.com/users/12193', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/53811', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4086/']