Finding sum of absolute differences of memory blocks

Discussion in 'C Programming' started by equinox1248, May 3, 2006.

  1. equinox1248

    equinox1248 Guest

    Hi,

    I thought this would be answered several time in this group, but I
    couldn't find anything relevant.

    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? Any
    tricks are appreciated. FYI, the memory blocks carry integer values.

    Thanks!
    equinox1248, May 3, 2006
    #1
    1. Advertising

  2. equinox1248

    Jack Klein Guest

    On 2 May 2006 19:45:26 -0700, "equinox1248" <>
    wrote in comp.lang.c:

    > Hi,
    >
    > I thought this would be answered several time in this group, but I
    > couldn't find anything relevant.
    >
    > What would be the most efficient way of calculating sum of absolute
    > values of two memory blocks?


    What do you mean by "memory blocks"? Do you mean what the C language
    laughingly calls "arrays"?

    > Consider A and B size of N:


    Now you have a terminology problem. What exactly are 'A' and 'B'? If
    you actually have "memory blocks", that is raw memory, the size of the
    number of bytes (char sized objects) it contains. On the other hand,
    if you have an array of some type, say int or double, it generally has
    two different sizes. The result of the sizeof operator on an array
    will yield the number of bytes it contains. But generally more useful
    is the number of elements it contains, which is the number of bytes in
    the array divided by the number of bytes in each element, that is
    sizeof array/sizeof (type of array).

    > 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? Any
    > tricks are appreciated. FYI, the memory blocks carry integer values.
    >
    > Thanks!


    How exactly do you expect to be able to compute a sum of values that
    you do not have in any way other than computing each value and adding
    it to a sum?

    By the way, if you are actually talking about arrays, defined like
    this:

    #define N some_number

    int A[N];
    int B[N];

    ....then your code snippet above has a serious mistake.

    Such arrays have N elements numbered 0 through N-1. Your loop is
    starting at element N, which is beyond the end of an N element array
    in C.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, May 3, 2006
    #2
    1. Advertising

  3. In article <>,
    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
    Walter Roberson, May 3, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juho Jussila

    Xslt sum of absolute values

    Juho Jussila, Nov 12, 2007, in forum: XML
    Replies:
    4
    Views:
    1,627
    Juho Jussila
    Nov 13, 2007
  2. Home_Job_opportunity
    Replies:
    0
    Views:
    485
    Home_Job_opportunity
    Jan 8, 2009
  3. Home_Job_opportunity
    Replies:
    0
    Views:
    568
    Home_Job_opportunity
    Jan 14, 2009
  4. matt
    Replies:
    1
    Views:
    244
    George Ogata
    Aug 6, 2004
  5. James Byrne
    Replies:
    3
    Views:
    543
    James Byrne
    Sep 14, 2010
Loading...

Share This Page