Application details analysis of c ++ standard library

Source: Internet
Author: User

The C ++ programming language has been favored by developers. The power of its functions makes it play an important role in the development field. The C ++ standard class library we introduce here is one of the basic application technologies.

  • C ++ Input and Output
  • Detailed description of C ++ address Operators
  • Interpretation of c ++ function transmission in three ways
  • Function implementation of C ++ clock ()
  • Explanation of the specific code application of C ++ timer

The C ++ standard class library is a set of templates that implement common structures and algorithms. For example, dynamic arrays is called vector), set, map, and so on. Using the C ++ standard class library can save you a lot of time writing and debugging those containers. As mentioned earlier, if you want to maximize system efficiency, you must pay attention to the specific implementation details of your C ++ standard class library.

In order to be able to correspond to the largest range of applications, the standard of the C ++ standard class library remains silent in the field of memory allocation. Every operation in the C ++ standard class library container has a certain efficiency guarantee. For example, to insert a set, it only takes O (log n) time. However, the memory usage of a container is not guaranteed.

Let's take a closer look at a very common problem in Game Development: You want to save a group of objects and we will call it an object list, although you do not have to save it in the list of C ++ standard class libraries), you usually require that each object have only one and only one in this table, in this way, you don't have to worry about accidentally inserting an existing unit operation into the container. The set in the C ++ standard class library ignores copies. All the insert, delete, and query speeds are O (log n). Is this a good choice?

Although the speed of most operations on the set is O (log n), there is still a potential crisis. Although the container memory usage depends on implementation, many implementations are implemented based on the red and black trees. On the red-black tree, each node of the tree is an element of the container. A common implementation method is to assign a node when each element is added to the tree, and release a node when each element is removed from the tree. Based on the frequency of insertion and deletion, the time spent on the memory manager will more or less affect the benefits you get by using the set.

Another solution is to use vector to store elements. vector ensures high efficiency in adding elements at the end of the container. This indicates that in fact, the vector only re-allocates the memory in the case of an accident, that is, it doubles when it is full. When using vector to save a list of different elements, you must first check whether the element already exists. If not, add it. It takes O (n) Time to check the whole vector, but the actual amount of time involved should be relatively small, because every element of the vector is stored continuously in the memory, therefore, checking the entire vector is actually an easy-to-cache operation. Checking the entire set will result in cache miss, because the elements stored separately on the red/black tree may be scattered in various corners of the memory. At the same time, we also noticed that set must maintain an additional set of tags to set the entire tree. If you want to save the object pointer, set may take 3 to 4 times the memory consumed by the vector.

The time consumed by the Set delete operation O (log n) seems to be very fast, if you do not consider calls to free. The deletion operation of the Vector consumes O (n) because each element is copied to the previous position from the beginning to the end of the deleted element. If all elements are pointers, this copy can be completed by a simple memcpy () function, which is quite fast. This is also the reason why the object pointer is usually stored in the C ++ standard class library container, rather than the object itself. If you directly Save the object itself, it will cause many additional constructor calls in many operations, such as deletion ).

Set and map are generally more troublesome than useful. If you are not aware of this, consider traversing a container. For example:

 
 
  1. for(Collection::iterator it = Collection.begin(); 
  2. it != Collection.end(); ++it) 

If the Collection is a vector, ++ it is a pointer auto-incrementing. However, if the Collection is a set or a map, ++ it includes accessing the next node on the red/black tree. This operation is quite complex and can easily cause cache miss, because the nodes in the tree are almost everywhere in the memory.

Of course, if you want to save a large number of elements in the container and perform many Member requests, the efficiency of set O (log n) can fully offset the memory consumption. Similarly, if you use containers occasionally, the efficiency difference here is very small. You should make some efficiency evaluations to know how much n will make the set faster. You may be surprised to find that vector is more efficient than set in most typical applications of the game.

This is not all the memory usage of the C ++ standard class library. Be sure to know whether the container has actually released its memory when you use the clear method. If not, memory fragments may be generated. For example, if you create an empty vector when you start the game, add elements during the game, and then call clear during the game restart, then the vector may not release all of its memory. This empty vector may still occupy the memory in the heap and turn it into fragments. If you really need to implement the game in this way, there are two solutions to this problem. First, you can call reserve () when creating a vector to reserve enough space for the maximum number of elements you may need. If this is not feasible, you can force the vector to completely release the memory:

 
 
  1. Vector V;  
  2. // … elements are inserted into V here  
  3. Vector().swap(v);  
  4. // causes v to free its memory 

Set, list, and map do not have this problem, because they allocate and release memory for each element separately.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.