c++ 11 vector push_back和emplace_back速度比较

对比两种push方法的效率及速度:

#include<stdio.h>
#include<time.h>
#include<iostream>
#include<vector>
int main()
{
    double s1,s2;
    int i,j;
    std::vector<int> a{};
    std::vector<int> b{};
    double pushback_time = 0;
    double emplace_back_time = 0;
for(int count = 0; count < 10 ;count ++)
{
    a.clear();
    s1=clock();
    for(i=1;i<=20000;i++){
        for(j=1;j<=20000;j++){
            a.push_back(1);
        }
    }
    s2=clock();
    pushback_time += (s2-s1)/CLOCKS_PER_SEC;
    printf("size a is %d\n", a.size());
    //printf("20000*20000 vector pushback运行时间:%.12f s\n\n",(s2-s1)/CLOCKS_PER_SEC);
}
printf("20000*20000 vector pushback运行时间:%.12f s\n\n",pushback_time/10);
for(int count = 0; count < 10 ;count ++)
{
    b.clear();
    s1=clock();
    for(i=1;i<=20000;i++){
        for(j=1;j<=20000;j++){
            b.emplace_back(1);
        }
    }
    s2=clock();
    emplace_back_time+= (s2-s1)/CLOCKS_PER_SEC;
    printf("size b is %d\n", b.size());
    
}
printf("20000*20000 vector emplace_back运行时间:%.12f s\n\n",emplace_back_time/10);
printf("pushback运行时间 - emplace_back运行时间:%.12f s\n\n", pushback_time/10 - emplace_back_time/10);
    s1=clock();
    for(i=1;i<=1000000;i++){
            a.push_back(1);
    }
    s2=clock();
    printf("size a is %d\n", a.size());
    printf("1000000 vector pushback运行时间:%.12f s\n\n",(s2-s1)/CLOCKS_PER_SEC);

    s1=clock();
    for(j=1;j<=1000000;j++){
            b.emplace_back(1);
    }
    s2=clock();
    printf("size b is %d\n", b.size());
    printf("1000000 vector emplace_back运行时间:%.12f s\n\n",(s2-s1)/CLOCKS_PER_SEC);

    return 0;
}

其运行结果为:

size a is 400000000
size a is 400000000
size a is 400000000
size a is 400000000
size a is 400000000
size a is 400000000
size a is 400000000
size a is 400000000
size a is 400000000
size a is 400000000
20000*20000 vector pushback运行时间:0.630000000000 s

size b is 400000000
size b is 400000000
size b is 400000000
size b is 400000000
size b is 400000000
size b is 400000000
size b is 400000000
size b is 400000000
size b is 400000000
size b is 400000000
20000*20000 vector emplace_back运行时间:0.636865600000 s

pushback运行时间 - emplace_back运行时间:-0.006865600000 s

size a is 401000000
1000000 vector pushback运行时间:0.002313000000 s

size b is 401000000
1000000 vector emplace_back运行时间:0.002176000000 s
 

 即在100万个数据上单次运行测试,emplace_back比pushback比它快0.000244s

在4亿的数量级上,10次运行,emplace_back比pushback慢。

针对不同类型的数据,如复合类型

struct my_pointcloud
{
    /* data */
    int ring = 0;
    double x = 0.;
    double y = 0.;
    double z = 0.;
    double time = 0.;
};

int main()
{
    double s1,s2;
    int i,j;
    std::vector<int> a{};
    std::vector<int> b{};
    std::vector<my_pointcloud> c{};
    std::vector<my_pointcloud> d{};
    my_pointcloud one_point;

    double pushback_time = 0;
    double emplace_back_time = 0;
    for(int count = 0; count < 10 ;count ++)
    {
        c.clear();
        s1=clock();
        for(i=1;i<=2000000;i++){
            c.push_back(one_point);  
        }
        s2=clock();
        pushback_time += (s2-s1)/CLOCKS_PER_SEC;
        printf("size c is %d\n", c.size());
        //printf("20000*20000 vector pushback运行时间:%.12f s\n\n",(s2-s1)/CLOCKS_PER_SEC);
    }
    printf("2000000 struct vector pushback运行时间:%.12f s\n\n",pushback_time/10);
    for(int count = 0; count < 10 ;count ++)
    {
        d.clear();
        s1=clock();
        for(i=1;i<=2000000;i++){
            
            d.emplace_back(one_point);

        }
        s2=clock();
        emplace_back_time+= (s2-s1)/CLOCKS_PER_SEC;
        printf("size d is %d\n", d.size());
        
    }
    printf("2000000 struct vector emplace_back运行时间:%.12f s\n\n",emplace_back_time/10);
    printf("struct pushback运行时间 - emplace_back运行时间:%.12f s\n\n", pushback_time/10 - emplace_back_time/10);

20万的数量级

size c is 200000
size c is 200000
size c is 200000
size c is 200000
size c is 200000
size c is 200000
size c is 200000
size c is 200000
size c is 200000
size c is 200000
200000 struct vector pushback运行时间:0.001487700000 s

size d is 200000
size d is 200000
size d is 200000
size d is 200000
size d is 200000
size d is 200000
size d is 200000
size d is 200000
size d is 200000
size d is 200000
200000 struct vector emplace_back运行时间:0.001356000000 s

struct pushback运行时间 - emplace_back运行时间:0.000131700000 s
 

200万级别

size c is 2000000
size c is 2000000
size c is 2000000
size c is 2000000
size c is 2000000
size c is 2000000
size c is 2000000
size c is 2000000
size c is 2000000
size c is 2000000
2000000 struct vector pushback运行时间:0.016266700000 s

size d is 2000000
size d is 2000000
size d is 2000000
size d is 2000000
size d is 2000000
size d is 2000000
size d is 2000000
size d is 2000000
size d is 2000000
size d is 2000000
2000000 struct vector emplace_back运行时间:0.016144300000 s

struct pushback运行时间 - emplace_back运行时间:0.000122400000 s

2000万级别

size c is 20000000
size c is 20000000
size c is 20000000
size c is 20000000
size c is 20000000
size c is 20000000
size c is 20000000
size c is 20000000
size c is 20000000
size c is 20000000
2000000 struct vector pushback运行时间:0.162116800000 s

size d is 20000000
size d is 20000000
size d is 20000000
size d is 20000000
size d is 20000000
size d is 20000000
size d is 20000000
size d is 20000000
size d is 20000000
size d is 20000000
20000000 struct vector emplace_back运行时间:0.159388800000 s

struct pushback运行时间 - emplace_back运行时间:0.002728000000 s
 

5000万

size c is 50000000
size c is 50000000
size c is 50000000
size c is 50000000
size c is 50000000
size c is 50000000
size c is 50000000
size c is 50000000
size c is 50000000
size c is 50000000
5000000 struct vector pushback运行时间:0.381235700000 s

size d is 50000000
size d is 50000000
size d is 50000000
size d is 50000000
size d is 50000000
size d is 50000000
size d is 50000000
size d is 50000000
size d is 50000000
size d is 50000000
50000000 struct vector emplace_back运行时间:0.364381700000 s

struct pushback运行时间 - emplace_back运行时间:0.016854000000 s

2000万级别的数据,在push时使用原始的pushback可能会慢2-5ms左右,若数据量为5000万级别,pushback可能会慢8-70ms左右,根据不同时间cpu的频率有所差异。

vector 的size问题

std::vector<int> a{};

求sizeof(a) = ?

    std::vector<int> a{};
    printf("init a size is %d\n", sizeof(a));

    std::vector<int> b{};
    std::vector<my_pointcloud> c{};
    std::vector<my_pointcloud> d{};
    my_pointcloud one_point;
    for(int k = 0; k < 100; k++)
    {
        a.push_back(1);
        printf("a size is %d, a.size() = %d, a.capacity() = %d.\n", sizeof(a), a.size(), a.capacity());
    }

init a size is 24
a size is 24, a.size() = 1, a.capacity() = 1.
a size is 24, a.size() = 2, a.capacity() = 2.
a size is 24, a.size() = 3, a.capacity() = 4.
a size is 24, a.size() = 4, a.capacity() = 4.
a size is 24, a.size() = 5, a.capacity() = 8.
a size is 24, a.size() = 6, a.capacity() = 8.
a size is 24, a.size() = 7, a.capacity() = 8.
a size is 24, a.size() = 8, a.capacity() = 8.
a size is 24, a.size() = 9, a.capacity() = 16.
a size is 24, a.size() = 10, a.capacity() = 16.
a size is 24, a.size() = 11, a.capacity() = 16.
a size is 24, a.size() = 12, a.capacity() = 16.
a size is 24, a.size() = 13, a.capacity() = 16.
a size is 24, a.size() = 14, a.capacity() = 16.
a size is 24, a.size() = 15, a.capacity() = 16.
a size is 24, a.size() = 16, a.capacity() = 16.
a size is 24, a.size() = 17, a.capacity() = 32.
a size is 24, a.size() = 18, a.capacity() = 32.
a size is 24, a.size() = 19, a.capacity() = 32.
a size is 24, a.size() = 20, a.capacity() = 32.
a size is 24, a.size() = 21, a.capacity() = 32.
a size is 24, a.size() = 22, a.capacity() = 32.
a size is 24, a.size() = 23, a.capacity() = 32.
a size is 24, a.size() = 24, a.capacity() = 32.
a size is 24, a.size() = 25, a.capacity() = 32.
a size is 24, a.size() = 26, a.capacity() = 32.
a size is 24, a.size() = 27, a.capacity() = 32.
a size is 24, a.size() = 28, a.capacity() = 32.
a size is 24, a.size() = 29, a.capacity() = 32.
a size is 24, a.size() = 30, a.capacity() = 32.
a size is 24, a.size() = 31, a.capacity() = 32.
a size is 24, a.size() = 32, a.capacity() = 32.
a size is 24, a.size() = 33, a.capacity() = 64.
a size is 24, a.size() = 34, a.capacity() = 64.
a size is 24, a.size() = 35, a.capacity() = 64.
a size is 24, a.size() = 36, a.capacity() = 64.
a size is 24, a.size() = 37, a.capacity() = 64.
a size is 24, a.size() = 38, a.capacity() = 64.
a size is 24, a.size() = 39, a.capacity() = 64.
a size is 24, a.size() = 40, a.capacity() = 64.
a size is 24, a.size() = 41, a.capacity() = 64.
a size is 24, a.size() = 42, a.capacity() = 64.
a size is 24, a.size() = 43, a.capacity() = 64.
a size is 24, a.size() = 44, a.capacity() = 64.
a size is 24, a.size() = 45, a.capacity() = 64.
a size is 24, a.size() = 46, a.capacity() = 64.
a size is 24, a.size() = 47, a.capacity() = 64.
a size is 24, a.size() = 48, a.capacity() = 64.
a size is 24, a.size() = 49, a.capacity() = 64.
a size is 24, a.size() = 50, a.capacity() = 64.
a size is 24, a.size() = 51, a.capacity() = 64.
a size is 24, a.size() = 52, a.capacity() = 64.
a size is 24, a.size() = 53, a.capacity() = 64.
a size is 24, a.size() = 54, a.capacity() = 64.
a size is 24, a.size() = 55, a.capacity() = 64.
a size is 24, a.size() = 56, a.capacity() = 64.
a size is 24, a.size() = 57, a.capacity() = 64.
a size is 24, a.size() = 58, a.capacity() = 64.
a size is 24, a.size() = 59, a.capacity() = 64.
a size is 24, a.size() = 60, a.capacity() = 64.
a size is 24, a.size() = 61, a.capacity() = 64.
a size is 24, a.size() = 62, a.capacity() = 64.
a size is 24, a.size() = 63, a.capacity() = 64.
a size is 24, a.size() = 64, a.capacity() = 64.
a size is 24, a.size() = 65, a.capacity() = 128.
a size is 24, a.size() = 66, a.capacity() = 128.
a size is 24, a.size() = 67, a.capacity() = 128.
a size is 24, a.size() = 68, a.capacity() = 128.
a size is 24, a.size() = 69, a.capacity() = 128.
a size is 24, a.size() = 70, a.capacity() = 128.
a size is 24, a.size() = 71, a.capacity() = 128.
a size is 24, a.size() = 72, a.capacity() = 128.
a size is 24, a.size() = 73, a.capacity() = 128.
a size is 24, a.size() = 74, a.capacity() = 128.
a size is 24, a.size() = 75, a.capacity() = 128.
a size is 24, a.size() = 76, a.capacity() = 128.
a size is 24, a.size() = 77, a.capacity() = 128.
a size is 24, a.size() = 78, a.capacity() = 128.
a size is 24, a.size() = 79, a.capacity() = 128.
a size is 24, a.size() = 80, a.capacity() = 128.
a size is 24, a.size() = 81, a.capacity() = 128.
a size is 24, a.size() = 82, a.capacity() = 128.
a size is 24, a.size() = 83, a.capacity() = 128.
a size is 24, a.size() = 84, a.capacity() = 128.
a size is 24, a.size() = 85, a.capacity() = 128.
a size is 24, a.size() = 86, a.capacity() = 128.
a size is 24, a.size() = 87, a.capacity() = 128.
a size is 24, a.size() = 88, a.capacity() = 128.
a size is 24, a.size() = 89, a.capacity() = 128.
a size is 24, a.size() = 90, a.capacity() = 128.
a size is 24, a.size() = 91, a.capacity() = 128.
a size is 24, a.size() = 92, a.capacity() = 128.
a size is 24, a.size() = 93, a.capacity() = 128.
a size is 24, a.size() = 94, a.capacity() = 128.
a size is 24, a.size() = 95, a.capacity() = 128.
a size is 24, a.size() = 96, a.capacity() = 128.
a size is 24, a.size() = 97, a.capacity() = 128.
a size is 24, a.size() = 98, a.capacity() = 128.
a size is 24, a.size() = 99, a.capacity() = 128.
a size is 24, a.size() = 100, a.capacity() = 128.

可以看到,sizeof(a)是不变的,a size()为 实际含有的数量,a.capacity()为预先分配的数量,为上一个数量的两倍,size达到容量时就会重新分配内存。

sizeof(vector)取决于vector类的具体实现,和编译器有关系,通过查看STL源码可以看到vector有四个成员变量 _A   allocator; iterator   _First,   _Last,   _End;

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

avenger_fang

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值