Compiler is your friend
Khem Raj September 12, 2024 #metaHow do you see the tools when developing software ? We write correct language primitives and syntax to represent the program logic. To get best out of the compiler, you can help it so it can help you back by generating best code for your program.
Remember, compilers are also software, it is given a algorithms coded in a language say C/C++ and it has to compile it with in a reasonable time and system constraints. Compilers use heuristics often to perform code transformations. If it is not able to compute a transformation confidently and within time, it will resort to more conservative strategy, because its number one job is to generate correct code, not fast code.
However, if you provide hints via your program then it can do a lot better job and generate better code. e.g. inline keyword, Pragmas, data alignment, vectorization hints etc. This helps compiler understand a lot more about your code than just the syntax and logic and do a better job.
Like any other, communication is the key here. Find ways to communicate better with the tool and in return you will get better code generation.