How expensive is copying a card?

I have map<EntityState, boost::weak_ptr<Animation>> in my EntityRepresentation class. I would like to create a Builder class for the presentation, but I should consider the cost of copying the map.

EntityState cheap to copy because it is a collection of static functions; boost::weak_ptr also cheap to copy. What about the card as a whole?

+6
source share
4 answers

It depends on the number of elements that it has. I do not think that his own members will cause many problems.

+2
source

Do not optimize prematurely. In many scenarios, the performance of the builder class execution will not be a bottleneck.

Typically, the difficulty of copying a card is O(n) . From the comments, it seems that n is small. If you have determined that you really need to optimize, then in this case using two vectors will be cheaper both when accessing elements and when copying.

+4
source

I am surprised that no one has mentioned Copy Elision yet.

This concept allows the compiler to copy a copy whenever possible. Thus, it is possible that the implementation of your builder will simply build your EntityRepresentation directly into the "return" slot and avoid all copies. At this point, your concern will be controversial.

+1
source

I would not worry about this until the profiler said what you need, but, generally speaking, the map will have one selection per element, which can have a significant impact.

0
source

Source: https://habr.com/ru/post/890384/


All Articles