Mark McIntyre wrote:
> On Mon, 26 Sep 2005 12:11:23 +0530, in comp.lang.c , "Neo"
> <> wrote:
>
>
>>Hi Folks,http://www.abarnett.demon.co.uk/tutorial.html#FASTFOR Page
>>states
>
>
> (that reversing loop order is faster)
>
> The page is talking rot. It *may* be faster. It *may* be slower. The
> only way to know is to benchmark your particular implementation in the
> specific case you're examining.
>
Actually, the page is talking rubbish about a great deal more than just
this case. It's full of generalisations that depend highly on the
compiler and target in question (the post is cross-posted to
comp.arch.embedded, so we are looking at a wide range of targets). "Use
switch instead of if...else..." (varies widely according to
target/compiler and the size of the switch), "Avoid ++, -- in while ()
expressions" (good compilers work well with such expressions), "Use
word-size variables instead of chars" (great for PPC, indifferent for
msp430, terrible for AVR), "Addition is faster than multiplication - use
'val + val + val' instead of 'val * 3' " (wrong for most compiler/target
combinations).
It's a nice idea to try to list such tips, but the page is badly out of
date, and makes all sorts of unwarranted assumptions.
So, as Mark says, benchmark your implementation. Also examine the
generated assembly code (you do understand the generated assembly? If
not, forget about such minor "optimisations".) And remember Knuth's
rules regarding such code-level optimisations:
1. Don't do it.
2. (For experts only) Don't do it yet.