美文网首页
performance of vector in c++

performance of vector in c++

作者: perryn | 来源:发表于2017-08-14 21:39 被阅读28次

1.many devloper can using vector in c++,but less know how to using vector high performance.if you don't to expand vector of using un running time,you can use vector.reserve (that is a function of vector) to avoid some performance ,you must use it as such rules:

 1. Allocate a new block of memory that is some multiple of the container's current capacity. In most implementations, vector and string capacities grow by a factor of two each time. i.e. their capacity is doubled each time the container must be expanded.
 2. Copy all the elements from the container's old memory into its new memory.
 3. Destroy the objects in the old memory.
 4. Deallocate the old memory.

/*************************************************************************
    > File Name: expance_vector.cc
  > Author: perrynzhou
  > Mail: perrynzhou@gmail.com
  > Created Time: Sat 12 Aug 2017 06:02:21 AM AKDT
 ************************************************************************/

#include <cstdint>
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
class FirstVector {
public:
    FirstVector(uint64_t max)
        : m_size(max)
    {
        cout << "FirstVector" << endl;
    }
    void put()
    {
        for (uint64_t i = 0; i < m_size; i++) {
            m_data.push_back(i);
        }
    }

private:
    vector<uint64_t> m_data;
    uint64_t m_size;
};
class SecondVector {
public:
    SecondVector(uint64_t max)
        : m_size(max)
    {
        m_data.reserve(m_size);
        cout << "SecondVector" << endl;
    }
    void put()
    {
        for (uint64_t i = 0; i < m_size; i++) {
            m_data.push_back(i);
        }
    }

private:
    uint64_t m_size;
    vector<uint64_t> m_data;
};
int main(int argc, char* argv[])
{
    if (argc != 3) {
        return -1;
    }
    uint64_t count = atoi(argv[2]);
    if (strncmp(argv[1], "0", 1) == 0) {
        FirstVector fv(count);
        fv.put();
    } else {
        SecondVector sv(count);
        sv.put();
    }
    return 0;
}

runing it ,the result as follows:

Jietu20170814-213849@2x.jpg

相关文章

  • performance of vector in c++

    1.many devloper can using vector in c++,but less know how...

  • 2_11基数排序

    C++的queue实现 C++ vector 实现 python 实现

  • C++ STL 之 vectot(三)

    今天我们继续更新 C++ STL 中 vector 容器的使用 vector 容器增加元素 vector 容器增加...

  • OJ刷题知识点

    C++ | vector vector:向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence ...

  • 顺序容器vector

    转自C++ vector的用法(整理)#include 一、vector初始化的五种方式 二、v...

  • 浅析C++的内联

    《Efficient C++ : Performance Programming Techniques》是一本关于...

  • 标准模板库-vector

    标准模板库-vector 1. vector简介 vector为C++的STL中的模板数组容器。在使用时需要包含#...

  • Value,Vector,Map

    Value:基本容纳了C++中的所有基本类型,个人觉得有点像auto。 Vector:和C++中的vector十分...

  • 2018-12-24 #STL#

    C++ vector删除符合条件的元素 两点:algorithm::remove,vector.erase()cp...

  • c++ list, vector, map, set 区别与用法

    c++ list, vector, map, set 区别与用法比较 List封装了链表,Vector封装了数组,...

网友评论

      本文标题:performance of vector in c++

      本文链接:https://www.haomeiwen.com/subject/qosprxtx.html