震惊!接受这4个小建议可以使你的C++程序性能提升80%!

2019-07-30  本文已影响0人  拉普拉斯妖kk

建议一:如果元素个数确定,使用reserve函数来提前为vector分配对象内存空间。

#include <iostream>
#include <vector>


class BigClass {
public:
    BigClass() {
        std::cout << "BigClass constructor" << std::endl;
    }
};

int main() {
    std::cout << "initialize vector with element number..." << std::endl;
    std::vector<BigClass> vec1(10);
    std::cout << "vec1 size:" << vec1.size() << std::endl << std::endl;

    std::cout << "vector resize..." << std::endl;
    std::vector<BigClass> vec2;
    vec2.resize(10);
    std::cout << "vec2 size:" << vec2.size() << std::endl << std::endl;


    std::cout << "vector reserve..." << std::endl;
    std::vector<BigClass> vec3;
    vec3.reserve(10);
    std::cout << "vec3 size:" << vec3.size() << std::endl << std::endl;

    system("pause");

    return 0;
}
initialize vector with element number...
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
vec1 size:10

vector resize...
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
BigClass constructor
vec2 size:10

vector reserve...
vec3 size:0

建议二:对于数据量比较大的vector,使用clear+shrink_to_fit函数来正确的释放内存。

#include<iostream>
#include<vector>

using namespace std;

int main() {
    vector<int> v {1,2,3,4,5};

    cout << "size:" << v.size() << endl;
    cout << "capacity:" << v.capacity() << endl;

    v.clear();
    cout << "after clear size:" << v.size() << endl;
    cout << "after clear capacity:" << v.capacity() << endl;

    cout << "v[0] = " << *v.data() << endl;

    system("pause");

    return 0;
}
size:5
capacity:5
after clear size:0
after clear capacity:5
v[0] = 1
#include<iostream>
#include<vector>

using namespace std;

int main() {
    vector<int> v{ 1,2,3,4,5 };

    cout << "size:" << v.size() << endl;
    cout << "capacity:" << v.capacity() << endl;

    v.swap(vector<int>());
    cout << "after swap size:" << v.size() << endl;
    cout << "after swap capacity:" << v.capacity() << endl;

    system("pause");

    return 0;
}
size:5
capacity:5
after swap size:0
after swap capacity:0
#include<iostream>
#include<vector>

using namespace std;

int main() {
    vector<int> v{ 1,2,3,4,5 };

    cout << "size:" << v.size() << endl;
    cout << "capacity:" << v.capacity() << endl;

    v.clear();
    v.shrink_to_fit();
    cout << "after clear+shrink_to_fit size:" << v.size() << endl;
    cout << "after clear+shrink_to_fit capacity:" << v.capacity() << endl;

    system("pause");

    return 0;
}
size:5
capacity:5
after clear+shrink_to_fit size:0
after clear+shrink_to_fit capacity:0

建议三:使用emplace_back替代push_back减少内存拷贝和移动。

#include <vector>  
#include <string>  
#include <iostream>  
#include <map>

struct Person {
    std::string name;
    std::string country;
    int year;

    Person(std::string p_name, std::string p_country, int p_year)
        : name(std::move(p_name)), country(std::move(p_country)), year(p_year) {
        std::cout << "I am being constructed." << std::endl;
    }
    Person(Person&& other)
        : name(std::move(other.name)), country(std::move(other.country)), year(other.year) {
        std::cout << "I am being moved." << std::endl;
    }
};

int main() {
    std::map<int, Person> m;
    std::cout << "map insert..." << std::endl;
    m.insert(std::make_pair(23333, Person("kk", "china", 9021)));

    std::cout << "map emplace..." << std::endl;
    m.emplace(23333, Person("kk", "china", 9021));

    std::vector<Person> v;
    std::cout << "vector push_back..." << std::endl;
    v.push_back(Person("kk", "china", 9021));

    std::vector<Person> v1;
    std::cout << "vector emplace_back..." << std::endl;
    v1.emplace_back("kk", "china", 9021);

    system("pause");

    return 0;
}
map insert...
I am being constructed.
I am being moved.
I am being moved.
map emplace...
I am being constructed.
I am being moved.
vector push_back...
I am being constructed.
I am being moved.
vector emplace_back...
I am being constructed.

建议四:在没有排序需求时,使用无序容器(unordered container)替代有序容器。

上一篇下一篇

猜你喜欢

热点阅读