I have a class containing a number of double values. This is stored in a vector where the indices for the classes are important (they are referenced from elsewhere). The class looks something like this:
Vector of classes
class A { double count; double val; double sumA; double sumB; vector<double> sumVectorC; vector<double> sumVectorD; } vector<A> classes(10000);
The code that needs to run as fast as possible is something like this:
vector<double> result(classes.size()); for(int i = 0; i < classes.size(); i++) { result[i] += classes[i].sumA; vector<double>::iterator it = find(classes[i].sumVectorC.begin(), classes[i].sumVectorC.end(), testval); if(it != classes[i].sumVectorC.end()) result[i] += *it; }
The alternative is instead of one giant loop, split the computation into two separate loops such as:
for(int i = 0; i < classes.size(); i++) { result[i] += classes[i].sumA; } for(int i = 0; i < classes.size(); i++) { vector<double>::iterator it = find(classes[i].sumVectorC.begin(), classes[i].sumVectorC.end(), testval); if(it != classes[i].sumVectorC.end()) result[i] += *it; }
or to store each member of the class in a vector like so:
Class of vectors
vector<double> classCounts; vector<double> classVal; ... vector<vector<double> > classSumVectorC; ...
and then operate as:
for(int i = 0; i < classes.size(); i++) { result[i] += classCounts[i]; ... }
Which way would usually be faster (across x86/x64 platforms and compilers)? Are look-ahead and cache lines are the most important things to think about here?
Update
The reason I’m doing a linear search (i.e. find) here and not a hash map or binary search is because the sumVectors are very short, around 4 or 5 elements. Profiling showed a hash map was slower and a binary search was slightly slower.
As lothar says, you really should test it out. But to answer your last question, yes, cache misses will be a major concern here.
Also, it seems that your first implementation would run into load-hit-store stalls as coded, but I’m not sure how much of a problem that is on x86 (it’s a big problem on XBox 360 and PS3).