??? 03/07/09 22:07 Read: times |
#163220 - Avoid variable aliasing if you like high optimization levels Responding to: ???'s previous message |
The RealView compiler is quite competent, if you just ask it to optimize.
But yes, the algorithm is always the #1 step when writing critical code. It doesn't matter how much you rev your car engine, if you forget to put it in the correct gear. In some situations, there are no alternatives for assembler, since a human is quite clever. But one problem with assembler is that people have a tendancy to go directly to assembler and write very efficient code with a bum algorithm. With C, it is way easier to experiment with different algorithms. Only after an analysis of the runtime behaviour of the different algorithms will it be possible to decide if assembler is needed or not - and then you will know that the code isn't just fast, but it is also doing the least amount of work. It isn't really true that compilers always produces correct code. The code generation is normally correct, but you have a big problem with aliasing. You must always be careful that you avoid accessing a variable in multiple ways. For example taking the address of a global variable and later calling a function that is using this global variable directly, but also gets a pointer to the same variable as input parameter. Compilers often have switches to control if it should be wary of aliasing or not, but you can loose quite a lot of speed if the compiler is required to generate defensive code. So if you plan on using max optimization, you should always write every single code line based on this knowledge. Never ever mix direct access and indirect access to the same variable unless it is in the same code block, so the compiler can see that you do it and when you do it. |