Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slow std::map for large entries

We have 48,16,703 entries in this format.

1 abc
2 def
...
...
4816702 blah
4816703 blah_blah

Since the number of entries are quite big, I am worried that std::map would take much time during insertion since it need to do the balancing as well for each insertion.

Only inserting these entries into the map takes a lot of time. I am doing

map[first] = second;

Two questions: 1. Am I correct in using std::map for these kind of cases? 2. Am I correct in inserting like the above way. OR Should I use map.insert()

I am sorry for not doing the experiments and writing the absolute numbers but we want an general consensus if we are doing the right thing or not.

Also, they keys are not consecutive always..

P.S. Of-course, later we will need to access that map as well to get the values corresponding to the keys.

like image 897
Rahul Bhargava Avatar asked Dec 10 '18 12:12

Rahul Bhargava


People also ask

Are maps slow in C++?

Maps are 'fast enough' but not brilliant for some cases. Try to analyze what is the structure of objects you need to store. If the fields are fixed I'd recommend not to use nested maps. At all.

What is the maximum size of a map in C++?

In C++, map::max_size returns the max. number of elements. In a vanilla map , there's an upper bound of at most SIZE_T_MAX elements, which is 2^64-1 on modern hardware.

Is map faster than vector C++?

The point where maps become faster than vectors depends on the implementation, on your processor, what data is in the map, and subtle things like what memory is in the processor's cache. Typically, the point where map becomes faster would be about 5-30 elements.

Is map faster than set?

The map solution results in "Time Limit Exceeded on Test 3", whereas the set solution results in "Time Limit Exceeded on Test 2", which means that Test 2 is such that the map solution works faster on it than the set solution.


1 Answers

If you don’t need to insert into the map afterwards, you can construct an unsorted vector of your data, sort it according to the key, and then search using functions like std::equal_range.
It’s the same complexity as std::map, but far less allocations.

like image 135
Dani Avatar answered Sep 30 '22 04:09

Dani