I'm using a std::map
to store about 20 million entries. If they were stored without any container overhead, it would take approximately 650MB of memory. However, since they are stored using std::map
, it uses up about 15GB of memory (i.e. too much).
The reason I am using an std::map
is because I need to find keys that are equal to/larger/smaller than x
. This is why something like sparsehash
wouldn't work (since, using that, I cannot find keys by comparison).
Is there an alternative to using std::map
(or ordered maps in general) that would result in less memory usage?
EDIT: Writing performance is much more important than reading performance. It will probably only read ~10 entries, but I don't know which entries it will read.
C++ std::map is an associative container, allowing you to store keys associated with values, for easy and efficient retrieval. It is part of the containers library of C++, as can be seen in cppreference.
Firstly, finding an item in a very small vector can easily be faster than the same thing in a map, because all the memory in a vector is always contiguous (and so plays more nicely with computers' caches and such things), and the number of comparisons needed to find something in a vector might be about the same as for ...
A map will automatically release resources when it's destroyed for anything allocated automatically. Unless you allocated the values with new , you don't delete them.
Many times inserting into the map will be faster, but it depends. The slowness of map insertion comes from two areas: Map's default allocator uses heap allocation, which is relatively slow. Replacing it with an efficient pool allocator can speed up allocation by more than x100.
Are you writing on-the-fly or one time before the lookup is done? If the later is the case, you shouldn't need a map, you could use std::vector
and one-time sort.
You could just insert everything unsorted to the vector, sort one-time after everything is there (O(N * log N) as well as std::map
, but much better performance characteristics) and then lookup in the sorted array (O(logN) as the std::map
).
And especially if you know the number of elements before reading and could reserve the vector size upfront, that could work pretty well. Or at least if you know some "upper bound" to reserve perhaps slightly more than actually needed but avoid the reallocations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With