473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

loop performance question

Hello,

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
Is there a way to improve the performance, or accessing data from
memory any faster in case of obj2?

Thanks in advance
Pertheli

-----------------------
// my object
struct MyObj{
double a;
double b;
};

// array of *MyObj
MyObj** objArray = new MyObj*[iMax]; // iMax = large > 20000

// create MyObj fill pointers int objArray

// for each possible pair in the array, do some testing
for (i=0; i<iMax; i++){
MyObj *obj1 = (MyObj*)objList[i];

for (j=i; j<iMax; j++){
MyObj *obj2 = (MyObj*)objList[j];

// do some tests
val += obj1->a; //-> very fast(same obj1)
val += obj1->b;

// same number of instruction but very slow
// accessing from different part in memory??
//val += obj1->a; //<- obj1 and obj2
//val += obj2->b; //<- Accessing any data of obj2 is very slow!
// 1/10th time slower than the above
}
}
Jul 22 '05 #1
3 3028
pertheli wrote:
Hello,

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
Is there a way to improve the performance, or accessing data from
memory any faster in case of obj2?

Thanks in advance
Pertheli

-----------------------
// my object
struct MyObj{
double a;
double b;
};

// array of *MyObj
MyObj** objArray = new MyObj*[iMax]; // iMax = large > 20000

// create MyObj fill pointers int objArray

// for each possible pair in the array, do some testing
for (i=0; i<iMax; i++){
MyObj *obj1 = (MyObj*)objList[i];

for (j=i; j<iMax; j++){
MyObj *obj2 = (MyObj*)objList[j];

// do some tests
val += obj1->a; //-> very fast(same obj1)
val += obj1->b;

// same number of instruction but very slow
// accessing from different part in memory??
//val += obj1->a; //<- obj1 and obj2
//val += obj2->b; //<- Accessing any data of obj2 is very slow!
// 1/10th time slower than the above
}
}


I would suspect that your compiler is optimising away the loop:

for (i=0; i<iMax; i++){
MyObj *obj1 = objList[i];

for (j=i; j<iMax; j++){
MyObj *obj2 = objList[j];
val += obj1->a; //-> very fast(same obj1)
val += obj1->b;
}
}

can be transformed into:

for (i=0; i<iMax; i++){
MyObj *obj1 = objList[i];

val += obj1->a * (iMax - i); //-> very fast(same obj1)
val += obj1->b * (iMax - i);
}

the second loop is needed once obj2 is added to the equation.

Try testing with optimisations turned off.

Michael Mellor
Jul 22 '05 #2
It doesn't seem right. Just using obj2 should slow down
things to that extent. Maybe some other stuff in the
inner loop is causing it.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - System Architecture Design CASE Tool
Jul 22 '05 #3
Hi,
"pertheli" <pe******@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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Edwards | last post by:
Hello, I have sort of a newbie question. I'm trying to optimize a loop by breaking it into two passes. Example: for(i = 0; i < max; i++) {
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
3
by: pertheli | last post by:
Hello, 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,...
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
1
by: Christopher DeMarco | last post by:
Hi all... I've written a class to provide an interface to popen; I've included the actual select() loop below. I'm finding that "sometimes" popen'd processes take "a really long time" to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.