P
pertheli
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;
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 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;
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
}
}