Performance implications of using a list of vectors versus a vector of vectors when appending in parallel

StackOverflow https://stackoverflow.com/questions/23443937

  •  14-07-2023
  •  | 
  •  

Domanda

It seems that in general, vectors are to be preferred over lists, see for example here, when appending simple types.

What if I want to fill a matrix with simple types? Every vector is a column, so I am going to go through the outer vector, and append 1 item to each vector, repeatedly.

Do the latter vectors of the outer vector always have to be moved when the previous vectors increase their reserved space? As in is the whole data in one continuous space? Or do the vectors all just hold a pointer to their individual memory regions, so the outer vector's memory size remains unchanged even as the individual vectors grow?

È stato utile?

Soluzione

Taken from the comments, it appears vectors of vectors can happily be used.

Altri suggerimenti

For small to medium applications, the efficiency of the vectors will seldom be anything to worry about.

There a couple of cases where you might worry, but they will be uncommon.

class CData {};  // define this
typedef std::vector<CData> Column;
typedef std::vector<Column> Table;

Table tab;

To add a new row, you will append an item to every column. In a worst case, you might cause a reallocation of each column. That could be a problem if CData is extremely complex and the columns currently hold a very large number of CData cells (I'd say 10s of thousands, at least)

Similarly, if you add a new column and force the Table vector to reallocate, it might have to copy each column and again, for very large data sets, that might be a bit slow.

Note, however, that a new-ish compiler will probably be able to move the columns from the old table to the new (rather than copying them), making that trivially fast.

As @kkuryllo said in a comment, it generally isn't anything to worry about.

Work on making your code as clean, simple and correct as possible. Only if profiling reveals a performance problem should you worry about optimising for speed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top