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
---------------
int i, j, k;
double val=0;
int iMax = 20000;
struct MyObj{
double a;
double b;
};
MyObj *o, *obj1, *obj2;
// array of *MyObj // iMax = large > 20000
MyObj** objList = (MyObj**)malloc(sizeof(MyObj*) * iMax);
// create MyObj fill pointers into objList
// for each possible pair in the array, do some testing
for (i=0; i<iMax; i++){
obj1 = (MyObj*)objList;
for (j=i; j<iMax; j++){
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
---------------
int i, j, k;
double val=0;
int iMax = 20000;
struct MyObj{
double a;
double b;
};
MyObj *o, *obj1, *obj2;
// array of *MyObj // iMax = large > 20000
MyObj** objList = (MyObj**)malloc(sizeof(MyObj*) * iMax);
// create MyObj fill pointers into objList
// for each possible pair in the array, do some testing
for (i=0; i<iMax; i++){
obj1 = (MyObj*)objList;
for (j=i; j<iMax; j++){
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
}
}