In article <. com>,
equinox1248 <> wrote:
>What would be the most efficient way of calculating sum of absolute
>values of two memory blocks?
>Consider A and B size of N:
>int temp=0;
>for (n=N;n>-1;n--)
> temp+= abs(A[n]-B[n]);
>How can one optimize this piece of code and make it run faster?
Leave the optimization to the compiler. Chances are that it can
do as well as you can for any one architecture -- and the
next compiler over on the next architecture is probably going
to know how to optimize for -that-, whereas any tricks you
specifically try to take advantage of on your existing system
might not work on the next platform.
There is something that should be fairly portable:
#define ABSDIFF(i,j) ((i)<(j)?(j)-(i)

i)-(j))
This has problems with side effects, but it saves on the
function call. On the other hand, the abs function call might
be coded as a single inline hardware instruction...
On some highly pipelined implementations, the macro I show
above can be fairly efficient, because the CPU can
"speculate" about the result of the comparison,
calculating both possible results and then "move conditional"
the proper value in -- all without taking any branchs. But that
doesnm't work for all CPU tpes`
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell