<p>Anyone know this compiler feature? It seems GCC support that. How does it work? What is the potential gain? In which case it's good? Inner loops?</p>

<p>(this question is specific, not about optimization in general, thanks)</p>

[{'answer_id': 52569, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 2, 'selected': False, 'text': "<p>The fun thing about optimization is that speed gains are found in the unlikeliest of places.</p>\n\n<p>It's also the reason you need a profiler, rather than guessing where the speed problems are.</p>\n\n<p>I recommend starting with a profiler (<code>gperf</code> if you're using GCC) and just start poking around the results of running your application through some normal operations.</p>\n"}, {'answer_id': 52586, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 2, 'selected': False, 'text': '<p>Jason\'s advise is right on.  The best speedups you are going to get come from "discovering" that you let an O(n<sup>2</sup>) algorithm slip into an inner loop somewhere, or that you can cache certain computations outside of expensive functions.</p>\n\n<p>Compared to the micro-optimizations that PGO can trigger, these are the big winners.  Once you\'ve done that level of optimization PGO might be able to help.  We never had much luck with it though - the cost of the instrumentation was such that our application become unusably slow (by several orders of magnitude).</p>\n\n<p>I like using Intel VTune as a profiler primarily because it is non-invasive compared to instrumenting profilers which change behaviour too much.</p>\n'}, {'answer_id': 57553, 'author': 'Greg Rogers', 'author_id': 5963, 'author_profile': 'https://Stackoverflow.com/users/5963', 'pm_score': 5, 'selected': True, 'text': '<p>It works by placing extra code to count the number of times each codepath is taken. When you compile a second time the compiler uses the knowledge gained about execution of your program that it could only guess at before. There are a couple things PGO can work toward:</p>\n\n<ul>\n<li>Deciding which functions should be inlined or not depending on how often they are called.</li>\n<li>Deciding how to place hints about which branch of an "if" statement should be predicted on based on the percentage of calls going one way or the other.</li>\n<li>Deciding how to optimize loops based on how many iterations get taken each time that loop is called.</li>\n</ul>\n\n<p>You never really know how much these things can help until you test it.</p>\n'}, {'answer_id': 71618, 'author': 'Dark Shikari', 'author_id': 11206, 'author_profile': 'https://Stackoverflow.com/users/11206', 'pm_score': 3, 'selected': False, 'text': '<p>PGO gives about a 5% speed boost when compiling x264, the project I work on, and we have a built-in system for it (make fprofiled).  Its a nice free speed boost in some cases, and probably helps more in applications that, unlike x264, are less made up of handwritten assembly.</p>\n'}]
['https://Stackoverflow.com/questions/52557', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1277510/']