Which vector and map use less memory (large dataset and unknown size)
I wonder which container uses less memory between std::map
and std::vector
with a larger dataset.
Loads of messages talk about efficiency, and my priority is not efficiency but memory consumption. So if we don't know the amount of our data (in my case there might be more than 12,000,000 records, each record is a string of 20 characters), is a map really better than a vector?
source to share
Depends on the problem you are solving. Basically, std :: vector uses string memory (since your data is large, make sure you have), but std :: map can take each node from separate pieces of memory. In contrast, std :: map uses more memory for the same data, as there is pointer manipulation between nodes.
source to share
A std::vector
must organize strings in contiguous memory. (The standard insists on this). Thus, the amount of contiguous memory in your example will be at least sizeof(string) * 12,000,000
for std::vector
. Fortunately, each line probably has its own buffer on the heap: 20 characters are around the truncation for implementations std::string
that use a fixed buffer for short strings.
std::map
will not have this touching problem, so this is probably the best container to use on this instance. But overall, this is likely to consume more memory. But this memory will be easier for the program.
source to share
If you want to store data in contiguous memory you have to go for std::vector
else, if you prefer a node based data structure and need a lot of insert and delete operations, I suggest instead of std::list
instead std::map
.
If you prefer a node based data structure that preserves the order between the elements of the data structure and don't have a key value pair, I prefer std::set
instead std::map
.
If you prefer a node based data structure with data as a key value pair, and the order between elements depends on the data key value, I prefer std::map
source to share