Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How expensive is it to copy a map?

I have a map<EntityState, boost::weak_ptr<Animation>> in my EntityRepresentation class. I would kinda want to create a Builder class for the representation, but I have to consider the costs of copying the map.

EntityState is cheap to copy since it's just a collection of static functions; boost::weak_ptr is also cheap to copy. How about the map as a whole?

like image 265
Paul Manta Avatar asked Jun 13 '11 06:06

Paul Manta


People also ask

Does map insert make a copy?

Yes -- when you insert an item into an std::map, you pass it by value, so what it contains is a copy of what you passed.


1 Answers

Don't optimize prematurely. In many scenarios the run-time performance of a builder class will not be the bottleneck.

Generally, the complexity of copying a map is O(n). From the comments, it looks like n is small. If you've identified that you really need to optimize, then in such a case, using two vectors will be cheaper both in accessing the items, and in the copying.

like image 179
Gilad Naor Avatar answered Oct 08 '22 14:10

Gilad Naor