Tilak Raj Singh
Tilak Raj Singh

Reputation: 353

For storage of sparse matrices which is better a vector or a vector for each row different

I an using a data structure for storing sparse matrices as follow. Like if I am having a matrix as follows enter image description here

I am using a vector of pairs to store the data. So is it better to store these value in a single vector like this:-

enter image description here

or will it be better to store them like this:-

enter image description here

I am using a seperate vector to store data about starting and end indexes for each row.

Which of the two methods will use less memory?????

Upvotes: 0

Views: 179

Answers (1)

Scott Jones
Scott Jones

Reputation: 2908

Because a vector allocates a single block of contiguous memory (unlike a list), a single vector would use less memory by consolidating the heap overhead. I assume the client interface is the same regardless (overloading operator[], for example), so your question is just about memory efficency.

Upvotes: 1

Related Questions