Coka
Coka

Reputation: 55

Difference between two type on insertion in unordered map

First i used make_pair to insert key and value in my unordered map . Then i checked my memory usage by pmap -x [pid] it was 535100 Kb

Now i change the insertion to map[key] = value Now memory usage is 535260 (increased) Can somebody explain difference bw these two insertion. so that i can understand the memory usage.

Upvotes: 0

Views: 106

Answers (3)

mkaes
mkaes

Reputation: 14119

You cannot tell the reason for this. It is implementation dependent. Which STL are you using which compiler? If possible look into the sources.

I can tell you for example that the memory consumption for the following statements is different on VS2010. But this an internal optimization.(The later uses less memory)

shared_ptr<int> i(new int(11));
auto j = make_shared<int>(11));

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254461

map.insert(make_pair(key,value));

searches the map for an entry with this key; if there is none, inserts a new entry with the given value; does not replace an existing value for this key.

map[key] = value

searches the map for an entry with this key; if there is none, inserts a new entry with a default-constructed value; then replaces the old (or newly-constructed) value with the new one by assignment.

So the second version might do more work, and might temporarily allocate more memory if the value type's default constructor does.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

The management of internal memory for the map is abstracted from you. You should not have to care about it, and you should not care about it.

Whatever internals are going on in your particular implementation are not to be concerned about.

Just use the provided API. Profile if you insist on tweaking for optimum use.

Possibilities include:

  • pre-allocation of a block of space
  • you made a mistake in your test (might be nice to see your testcases rather than just hearing you complain about them...)

Upvotes: 0

Related Questions