This document has analyzed std :: Deque, and provides a guiding ideology: When considering memory allocation and execution performance, use std :: Deque than std :: Vector.
Introduction
The std :: Deque container is studied in depth. This article will discuss in some cases use Deque> better than Vector. After reading this article, the reader should be able to understand the Different performance of DEQUE and Vector in memory allocation and performance during capacity growth. Since Deque> and Vector usage is very similar, the reader can refer to the Vector documentation to describe how to use STL containers.
DEQUE Total Exhibition
Deque and Vector are content in the standard template library. Deque is a double-end queue that is very similar to the Vector, which can be directly replaced in many ways. If the reader has been able to effectively use the VECTOR container, provide the member function and operation of Deque, and perform comparison references.
DEQUE member function
Function Description C.ssign (BEG, END) C.ssign (n, elem assigns data in the [BEG; END) section to C. The copy of N ELEM is assigned to C. C.At (IDX) Removes the data referred to in index IDX, if the IDX offline, throw out_of_range. C.back () The last data is passed, and does not check if this data exists. C. Begin () Retrieves a data that iterator is heavy. C. Clear () Removes all the data in the container. Deque
DEQUE operation
Function Description Operator [] Returns a reference to the specified location in the container.
The above features are obviously similar to the Vector, so we will make the following questions.
Question: Which one we use if DEQUE and VECTOR can provide the same function?
Answer: If you want to ask, use the Vector.
Or you give an explanation?
I am very happy to ask, it is indeed, this is not born, in fact, this problem is explained in the C standard, and there is a piece below: Vector is a typical method of use by default, for Deque, when inserted It is a better choice when deleting the operation.
Interestingly, this article is to understand this sentence very thoroughly.
what is new?
Read the above two forms, you will find two functions here and the vector comparison.
1, C.PUSH_FRONT (ELEM) - Insert a data at the head.
2, c.POP_FRONT () - Delete header data.
The call method is the same as C.PUSH_BACK (ELEM) and C.POP_BACK (), which will tell us that it is very useful for Deque>, and Deque can join data before and after. >
What is missing?
At the same time, you will also find two functions relative to Vector>, you will learn that DEQUE> does not require them.
1, Capacity () - Returns the current capacity of the Vector.
2, reserve () - Ges to the specified size Vector> allocated space.
Here is the beginning of our truly study, here, DEQUE> and Vector are completely different when managing internal storage. DEQUE is a large block allocation of memory, plugged into a fixed number of data. Vector is close to allocation memory (this may not be a bad thing). But we should pay attention to that the current memory is not enough when the volume is large enough. The following experiments to verify that demine does not require Capacity () and reserve ()> It is very reasonable.
Experiment 1 - Growth Container
purpose
The aim is to observe the difference between DEQUE and VECTOR when the capacity increases. With graphics, they differ in allocation of memory and execution efficiency.
description
The test program of this experiment is to read text content from a file. Each line is inserted into the Deque> and Vector per line. In the multi-read file, the plurality of data are read, and this class is to test this. content:
#include
result
Test program running platform and some conditions:
CPU 1.8 GHz Pentium 4 Memory 1.50 GB Operating System W2K-SP4 Number 9874 Average Each Ring Alphabet 1755.85 Read Files 45 A total of 4544330 Use the Windows Task Manager to record execution efficiency, this The CDURATION class of Laurent Guinnard is used in the program. The consumption system resources are as follows:
Note how the vector allocates memory, how is the highest value when allocating memory is allocated, and Deque is like this. It is inserted in the data, and the memory line grows. First, the memory allocation unit of DEQUE is reclaimed, If we want, we hope that its allocation of the memory looks like vector. Through the above test we need further testing, it is now proposed: Assuming that the memory allocated by the demire is not continuous, it must be released and recovered, we will These assumptions are added in the following tests, but first analyze this experiment from the performance appearance of the performance.
How long does it take to distribute memory?
Note Looking at the picture below, the Vector is looking for more memory when not inserting the data.
At the same time, we also noticed that the time to insert a set of data consumption using PUSH_BACK, note that each inserted set of data represents 9874 strings, and the average length of each string is 1755.85.
Experiment 2 - Vector :: RESERVE () Resource purpose
The purpose of this experiment is to call RESERVE () and Deque before adding a lot of data. How about the memory allocation and execution efficiency?
description
The test in this experiment is basically the same as the experiment, in addition to the following line of code in the constructor of the test class:
m_vdata.reserve (1000000);
result
Test program running platform and some conditions:
CPU 1.8 GHz Pentium 4 Memory 1.50 GB Operating System W2K-SP4 file number 9874 Average per line alphabetic number 1755.85 Read files 70 Total Insert data 691180
Using the Windows Task Manager to record execution efficiency, use the CDuration class of> Laurent Guinnard in this program. The consumption system resources are as follows:
We noticed that VECTOR did not need to distribute excess time allocation memory, this is because we used RESERVE () for the tested> 691180 data for us to insert a large amount of data, there is enough memory space, for Deque The assignment of storage allocation, observes the memory allocation graphics in this test and the previous graphics, and we need to further quantify this test.
How to improve the performance of memory allocation?
Below this legend explains that the capacity is increasing as data:
When adding data, the increase in capacity is basically the same, however, the vector has some sporadic time consumption when inserting data, look at the following episode:
Through statistical analysis, Vector and Deque are spent on the insertion average> 9874 data, the following is a summary form:
Vectordeque
Mean0.603724814 secMaximum0.738313000 secMinimum0.559959000 secStd. Dev0.037795736 sec6-Sigma0.226774416 sec
Mean0.588021114 sec maximum0.615617000 second.567503000 second.009907800 sec6-sigma0.059446800 Sec400 SEC
Experiment three - memory recycling
purpose
This experiment is that the memory assigned by the demion is not approaching, and it is difficult to recycle quantitative test analysis. description
In this experiment, the code in the experiment is used again, and the efficiency of adding records to add data execution in the call function is added to the following:
For (xrun
result
This test is running on the same platform, in addition to the inserted data from> 9874 to> 691180, you need to insert> 70 times, the following illustration shows> Deque in the case where the data is allocated, in Deque Insert a average string of each length of> 1755.85. >
Although the actual consumption time seen from several graphs is different, some curve are accurate to> R2 = 95.15%. The given data points actually deviate from the graph data of the statistics in the table below:
Deque resultsmean0.007089269 sec maximum11.02838496 SECMINIMUM-15.25901667 SecSTD. dev3.3803636 sec6-sigma20.2821816 second
In the same case, it is very meaningful to compare the results of the Vector. The following figure is a time comparison of the time comparison of the memory consumption of the Vector and DEQUE in the same case:
These data are> R2 = 82.12% in this test. This may be more optimized by each point repeatedly, in which this data is properly labeled, the data points actually depart from the graph data of the statistics in the table below:
Vector resultsmean-0.007122715sec maximum0.283452127 SECMINIMUM-0.26724459secstd. dev0.144572356sec6-sigma0.867434136sec
Experiment 2 - Vector :: RESERVE () Resource purpose
The purpose of this experiment is to call RESERVE () and Deque before adding a lot of data. How about the memory allocation and execution efficiency? description
The test in this experiment is basically the same as the experiment, in addition to the following line of code in the constructor of the test class:
m_vdata.reserve (1000000);
result
Test program running platform and some conditions:
CPU 1.8 GHz Pentium 4 Memory 1.50 GB Operating System W2K-SP4 file number 9874 Average per line alphabetic number 1755.85 Read files 70 Total Insert data 691180
Using the Windows Task Manager to record execution efficiency, use the CDuration class of> Laurent Guinnard in this program. The consumption system resources are as follows:
We noticed that VECTOR did not need to distribute excess time allocation memory, this is because we used RESERVE () for the tested> 691180 data for us to insert a large amount of data, there is enough memory space, for Deque The assignment of storage allocation, observes the memory allocation graphics in this test and the previous graphics, and we need to further quantify this test.
How to improve the performance of memory allocation?
Below this legend explains that the capacity is increasing as data:
When adding data, the increase in capacity is basically the same, however, the vector has some sporadic time consumption when inserting data, look at the following episode:
Through statistical analysis, Vector and Deque are spent on the insertion average> 9874 data, the following is a summary form:
Vectordeque
Mean0.603724814 secMaximum0.738313000 secMinimum0.559959000 secStd. Dev0.037795736 sec6-Sigma0.226774416 sec
Mean0.588021114 sec maximum0.615617000 second.567503000 second.009907800 sec6-sigma0.059446800 Sec400 SEC
Experiment three - memory recycling
purpose
This experiment is that the memory assigned by the demion is not approaching, and it is difficult to recycle quantitative test analysis.
description
In this experiment, the code in the experiment is used again, and the efficiency of adding records to add data execution in the call function is added to the following:
For (xrun
This test is running on the same platform, in addition to the inserted data from> 9874 to> 691180, you need to insert> 70 times, the following illustration shows> Deque in the case where the data is allocated, in Deque Insert a average string of each length of> 1755.85. >
Although the actual consumption time seen from several graphs is different, some curve are accurate to> R2 = 95.15%. The given data points actually deviate from the graph data of the statistics in the table below:
Deque resultsmean0.007089269 sec maximum11.02838496 SECMINIMUM-15.25901667 SecSTD. dev3.3803636 sec6-sigma20.2821816 second
In the same case, it is very meaningful to compare the results of the Vector. The following figure is a time comparison of the time comparison of the memory consumption of the Vector and DEQUE in the same case:
These data are> R2 = 82.12% in this test. This may be more optimized by each point repeatedly, in which this data is properly labeled, the data points actually depart from the graph data of the statistics in the table below:
Vector resultsmean-0.007122715sec maximum0.283452127 SECMINIMUM-0.26724459secstd. dev0.144572356sec6-sigma0.867434136sec