Lookup speed of data in a vector of classes vs. two-dimensional vector

Oct 27, 2011 at 6:55pm
Hi all,

I have a program which first creates a large (~100MB) table of values, and then spends the rest of its time looking up data from that table. Right now, the table is organized of a vector of classes, each of which contains a length-three array. The code is something like this:

class Data{
private:
int x[3];
public:
int getX(i); //i=0,1,2, so this returns the element of x requested
//some other stuff
};

vector<Data> values;
values=resize(N); //N some large number

//initialize all the elements of values

for( ... ){ //loop for a long time
values[i].getX(j); //j=0,1 or 2
}

My question is: is there a speed penalty for organizing the data as a vector of classes, as opposed to a two-dimensional vector? I believe the limiting factor on the speed of the code is how fast it can do these lookups, so I want to know the fastest way to do the lookup. On the other hand, keeping the data as an object of a class is the easiest way to organize the code.

Any help would be appreciated.
Oct 27, 2011 at 7:17pm
is there a speed penalty for organizing the data as a vector of classes, as opposed to a two-dimensional vector
The way you're doing it now is probably faster and definitely more compact memory-wise.
Last edited on Oct 27, 2011 at 7:22pm
Oct 27, 2011 at 7:20pm
What do you mean by lookups? Do you mean accessing a certain object by its indice, or finding a specific object based on some condition? There will be very little speed penalty for accessing objects by their indices, but iteratively searching through every object to find a specific object based off some condition would be rather slow.
Oct 27, 2011 at 7:43pm
I just mean looking things up by index. It sounds like they way I am doing it now is good, so thanks for the advice.
Topic archived. No new replies allowed.