Efficienct structure array

S

some one

Which of the following definition is more efficient?
struct TestData {
int a;
int b;
int var1[200];
int var2[200];
int var3[200];
int var4[200];
int var5[200];
int var6[200];
int var7[200];
int var8[200];
int var9[200];
};

OR

struct Test1Struct{
int a;
int b;
struct Test2Struct *Test2;
}Test1;

struct Test2Struct {
int var1;
int var2;
int var3;
int var4;
int var5;
int var6;
int var7;
int var8;
int var9;
} Test2[200];
 
K

Kevin D. Quitt

Depends on what you mean by efficient. The former may generate more
cache-coherent memory references if you are (e.g.) looping through var9,
while the latter may be better if you're working with the different varX's
with the same index.

That assumes your system has and uses cache, and that the compiler doesn't
step on its toes. But none of this is relevant to c.l.c.
 
B

Barry Schwarz

Which of the following definition is more efficient?
struct TestData {
int a;
int b;
int var1[200];
int var2[200];
int var3[200];
int var4[200];
int var5[200];
int var6[200];
int var7[200];
int var8[200];
int var9[200];
};

OR

struct Test1Struct{
int a;
int b;
struct Test2Struct *Test2;
}Test1;

struct Test2Struct {
int var1;
int var2;
int var3;
int var4;
int var5;
int var6;
int var7;
int var8;
int var9;
} Test2[200];

The two are not equivalent. An object of type struct TestData
contains 1802 ints. An object of type struct Test2Struct contains 2
ints and a pointer. The pointer must be initialized to point to a
struct Test2Struct.

If you changed the definition of Test1Struct to
struct Test2Struct Test2[200] /* no asterisk */;
then the two would probably be the same size but you would still have
to type more to reference one of the array elements:
struct TestData td;
struct Test1Struct t1s;
td.var6[5] = 3;
t1s.Test2[5].var6= 3;


<<Remove the del for email>>
 
E

EventHelix.com

The array-of-structures approach is better than the structure-with-arrays
approach from two points of view:

- Array-of-structures is probably closes to the problem domain. You have
a test record structure and an array of several test records is more
readable than the other option.

- Array-of-structures should be more efficent. Consider the case where
you are running a for loop over the entire index range. In each iteration
you just need to get the structure pointer once and then can work
on any entry in that structure.

Sandeep
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top