Due to the advances in x86 C compilers (namely GCC and Clang), many coding practices that were believed to improve efficiency are no longer used since the compilers can do a better job optimizing the code than humans (e.g. bit shift vs. multiplication).
Which specific practices are these?
Compiler optimizing process should meet the following objectives : The optimization must be correct, it must not, in any way, change the meaning of the program. Optimization should increase the speed and performance of the program. The compilation time must be kept reasonable.
Built-in compiler optimizations include one or more of the following: using options at the command-line and front-end of the tools; compiler-recognized keywords used at the source level (for example, using __packed might tell a compiler to reorder some declared global data in memory to eliminate padding) and automatic ...
Of the optimizations which are commonly recommended, a couple which are basically never fruitful given modern compilers include:
Modern compilers understand mathematics, and will perform transformations on mathematical expressions where appropriate.
Optimizations such as conversion of multiplication to addition, or constant multiplication or division to bit shifting, are already performed by modern compilers, even at low optimization levels. Examples of these optimizations include:
x * 2 -> x + x x * 2 -> x << 1
Note that some specific cases may differ. For instance, x >> 1
is not the same as x / 2
; it is not appropriate to substitute one for the other!
Additionally, many of these suggested optimizations aren't actually any faster than the code they replace.
I'm not even sure what to call this, but tricks like XOR swapping (a ^= b; b ^= a; a ^= b;
) are not optimizations at all. They're just party tricks — they are slower, and more fragile, than the obvious approach. Don't use them.
register
keywordThis keyword is ignored by many modern compilers, as it its intended meaning (forcing a variable to be stored in a register) is not meaningful given current register allocation algorithms.
Compilers will automatically perform a wide variety of code transformations where appropriate. Several such transformations which are frequently recommended for manual application, but which are rarely useful when applied thus, include:
static
, and it'll usually be inlined where appropriate when optimization is enabled.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With