STL container vector, list, deque Performance Comparison

Source: Internet
Author: User

C ++'s STL Template Library provides three container classes: vector, list, And deque.
These three containers often make us confused about which one should be used to implement our logic.
In a program with a small amount of data operations, the difference is not great,
However, when a certain amount of data is reached, the performance may be significantly different.

This article attempts to discuss this issue from two aspects: Introduction and performance comparison.

 

    1. Vector-Automatically increasing array

 

    1. List-good at inserting deleted linked lists

 

    1. Deque-Dual-end queue with the advantages of vector and list

 

    1. Performance arena

 

    1. Performance summary and usage suggestions

 

    1. Test Procedure List

 

 

Vector-Automatically increasing array

Vector is also called a Vector Array. It is used to solve the problem that the array defined in the program is
Cannot dynamically change the size.
The general program implementation is to create a fixed-length array at the same time when the class is created,
As the data is constantly written, once the array is filled up, a larger memory zone is re-opened,
Copy the original data to the new memory zone and discard the original memory.

As the program automatically manages the growth of arrays, it is indeed much easier for our programmers,
Just insert the data into it. Of course, the physical memory and virtual memory are plugged out.
The operating system is in trouble for you :-)

Vector can only forward as the array grows. Therefore, it only provides backend insertion and deletion,
That is, push_back and pop_back. Of course, you can also operate data on the front end and in the middle,
Insert and erase are used, but operations on data in the front end and in the middle will inevitably lead to the movement of data blocks,
This has a very high impact on performance.

The biggest advantage of all arrays is random access.
In vector, the AT and [] operators are provided for random access.
Because each data size is the same and arranged in the memory at no interval,
Therefore, to operate on a certain data, you only need to use an expression to directly calculate the address:
Address = base + Index * datasize

Similarly, you do not need to make great effort to open up, initialize, and clear the memory of the vector,
There is only one piece of memory from start to end.

 

List-good at inserting deleted linked lists

There are black and white, and everything in the world appears in pairs.
The linked list is the opposite for arrays.
The array itself does not have the dynamic growth capability (the program must re-open the memory for implementation ),
What makes linked lists powerful is the ability to dynamically increase and delete resources.
However, linked lists are weak for the powerful random access capabilities of arrays.

List is the implementation of a two-way linked list.
To provide two-way traversal capabilities, a list has two more pointers before and after the normal data unit.
This is also impossible. After all, the current PC memory structure is a large array,
The linked list requires more space to implement its own functions in different environments.

List provides four methods: push_back, push_front, pop_back, and pop_front.
To facilitate the addition and deletion of data at both ends of the list, but the AT and [] operators of the vector are missing.
Method for random data access. It is not impossible to implement, but the list designer.
I don't want the list to do those things, because they will do very poorly.

For list, clearing all elements in a container is a hard task,
Because the memory of all data units is not continuous, the list can only be deleted one by one through traversal.

 

Deque-Dual-end queue with the advantages of vector and list

Black and white, between these two extremes, are pleasant colors.
As a combination of vector and list, deque does have extraordinary strength.

The deque Implementation of STL has not been seen, but according to my own guesses,
The array should be segmented, and pointers should be added to the segmented array to link all segments,
Eventually it becomes a large array.

Like list, deque provides push_back, push_front,
Pop_back and pop_front methods. As you can imagine, if you want to operate on both ends of deque,
That is to say, we need to re-allocate the memory zone for the fixed-length array of the first and last segments,
Because the array of segments is small, the overhead of reallocation is not large.

Deque provides at and [] operators like vector.
If you want to calculate the address of a data, it is a little more troublesome than vector,
But the efficiency is much higher than list.
First, it traverses the array in the same way as list. The size of each array is accumulated during each traversal,
When traversing a segment and basen <= index <basen + basen_length,
You can use address = basen + basen_index to calculate the address.
Because the length of the Post-segment linked list is not very long
The overall performance has little impact.

It looks like deque is invincible, but deque is the same as argis in Greek mythology,
It also has its own weakness, which can be seen in subsequent test data.

P.s. Please search for "argiis heel" For details

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.