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.
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.