在vector末尾插入元素时,push_back比insert快

之前编程是偷懒,在vector后边添数据都用insert(xx.end(),xxx)写的。今天看了几篇vector插入原理的文章,感觉其与正宗的push_back应该有区别,就自己写了个代码验证下:
#include
#include
#include
using namespace std;
int main()
{
vector b;
clock_t start_time1 = clock();
for(int i =0 ;i<100000;i++)
b.push_back(1);
clock_t end_time1 = clock();
cout<<"CLOCKS_PER_SEC "< cout<<"time consumed of push_back "<<(double)(end_time1 - start_time1)/CLOCKS_PER_SEC< vector a;
clock_t start_time = clock();
for(int i =0 ;i<100000;i++)
a.insert(a.end(),1);
clock_t end_time = clock();
cout<<"time consumed of insert "<<(double)(end_time - start_time)/CLOCKS_PER_SEC< return 0;
}
同样插入10w个点,push_back还是比insert快的,有图为证
在vector末尾插入元素时,push_back比insert快_第1张图片

你可能感兴趣的:(在vector末尾插入元素时,push_back比insert快)