Is there a method to automatically find the best compiler options (on a given machine), which result in the fastest possible executable?
Naturally, I use g++ -O3
, but there are additional flags that may make the code run faster, e.g. -ffast-math
and others, some of which are hardware-dependent.
Does anyone know some code I can put in my configure.ac
file (GNU autotools), so that the flags will be added to the Makefile automatically by the ./configure
command?
In addition to automatically determining the best flags, I would be interested in some useful compiler flags that are good to use as a default for most optimized executables.
Update: Most people suggest to just try different flags and select the best ones empirically. For that method, I'd have a follow-up question: Is there a utility that lists all compiler flags that are possible for the machine I'm running on (e.g. tests if SSE instructions are available etc.)?