I’m writing code that uses a large matrix where the elements are a user defined class. To build this matrix, I use the following vector of vectors.
using namespace std;
vector< vector< userclass > > matrix = vector<vector<userclass> >(sizeX, vector<userclass>(sizeY));
This class, which might also be a struct, will contain a few builtins such as floats and pointers. So here’s the thing:
Let’s say the matrix will have a size of 2000 in one direction, but only a size of 20 in the other, but I have total freedom to choose which one. For the best performance, which one should I make the biggest, sizeX or sizeY?
In other words: which is faster, a small vector of large vectors, or a large vector of small vectors? Is there a difference at all?
The performance optimization should be towards single random accesses.
You should aim for the least number of vectors possible, which means that
sizeYshould be larger thansizeXfor best cache performance, not to mention taking up less space.Of course, it depends on how you intend to use them. If you can, try to stay accessing a vector for as long as possible –
vec[i][j]is much better thanvec[j][i]. If you have to dovec[j][i]then havingsizeXbe larger may have better performance, or using 1 contiguous array.Fastest iterating where
sizeX>sizeY: