Re: loop performance question

From: Ivan Vecerina (please_use_web_form_at_ivan.vecerina.com)
Date: 01/30/04


Date: Fri, 30 Jan 2004 09:46:41 +0100

Hi,
"pertheli" <pertheli@hotmail.com> wrote...
| I have a large array of pointer to some object. I have to run test
| such that every possible pair in the array is tested.
| eg. if A,B,C,D are items of the array,
| possible pairs are AB, AC, AD, BC and CD.
|
| I'm using two nested for loop as below, but it is running very slow
| whenever I access any data in the second loop.
|
| I suspect, time taken to fetch the obj2 from memory is the bottleneck
This would definitely be the case in the artificial sample you posted.
| Is there a way to improve the performance, or accessing data from
| memory any faster in case of obj2?

First advice: if you can, use a vector of objects instead of a vector
of pointers. The pointers to separately allocated objects break the
contiguity of memory accesses. Several platforms are optimized for
reading memory sequentially (as is the case with array/std::vector).
Also, make sure that your object are self-contained and do not contain
pointers to separately stored data (incl. containers or std::string).
This step is important for further optimizations, it is worth the
extra effort (even if you need a first pass to translate the data).

Next, I'll assume that this has to be an O(N2) problem: that you have
excluded alternative algorithms, or the possibility of pre-sorting the
objects in a way that allows to select a subset of object pairs to be
tested (e.g. geometrical clusters for collision detection, etc).

Then comes the question of how to avoid cache misses when accessing obj2.
The basic idea would be to compare sets of objects that simultaneously
fit into the processor cache.
This could be achieved with a loop such as:
  const int pageStep = 10;
  for( i=0; i<iMax; ) { // 1
    iEnd = i+pageStep; if(iEnd>iMax) iEnd=iMax;
    for( j=i; j<iMax; ) { // 2
      jStart = j; // as in your code, self-comparison assumed desireable
      jEnd = i+pageStep; if(jEnd>iMax) jEnd=iMax;
      for( ; i<iEnd ; ++i ) { // 3
        MyObj& obj1 = objList[i];
        for( j=jStart ; j<jEnd ; ++j ) { // 4
          MyObj& obj2 = objList[j];
          //... do stuff
        }
        if( jStart==i ) ++jStart; // for first iteration of loop 2
      }
    }
  }
  //Note: typed on-the-fly, not tested

Loops 3+4 compare objects pairs across two subsets of contiguous
objects, which are selected by loops 1+2.
Increasing the pageStep value will eventually improve performance
until the contiguous subsets are too large to fit in the cache
(or rather: one of the caches) - you may want to play with this.
The addition of some loop unrolling within 3&4 could also
help performance.

This is as far as I would go in C/C++: the code quickly gets
complicated, and the optimization is already platform-specific.

You should also try to use a specialized/optimizing compiler
(e.g. intel C++ on the x86 platform). Then look into the
assembly code itself...

This was to answer your specific question. But as always, it is
often more effective to choose a faster approach to a problem
than to tune code...

I hope this helps,
Ivan

-- 
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com


Relevant Pages

  • Re: newbie question
    ... The second assigns a pointer to an array 1000 of char to an int pointer. ... What you pass are the file pointers, ... > second file is identical to the first. ... Look at your while loop. ...
    (comp.lang.c)
  • Re: Thats not the Answer to Your Question
    ... You are probably unwittingly accessing off the end of the array and ... The code has an avoidable multiple access to the same cache line in ... loop in a more cache unfriendly way. ... make this trivial optimisation (even some basic interpretters can ...
    (sci.image.processing)
  • Re: Can this type of cache miss be reduced?
    ... as loop interchange, loop blocking, etc. ... But, for a large one-dimensional array, suppose the elements are only ... the funis doing, and your definition of "cache miss." ... compilers could insert extra code into loops to do ...
    (comp.compilers)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • Re: Why does this work? (overloads)
    ... one type to an array originally declared with another type. ... pointers to a common unconstrained array type, ... x: A renames c(0 .. ... for k in v'range loop ...
    (comp.lang.ada)