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

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

#include
#include
#include
#include
int main()
{
    double s1,s2;
    int i,j;
    std::vector a{};
    std::vector 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 a{};
    std::vector b{};
    std::vector c{};
    std::vector 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 a{};

求sizeof(a) = ?

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

    std::vector b{};
    std::vector c{};
    std::vector 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;

你可能感兴趣的:(c++,开发语言)