- 性能优化的2-8准则:20%的代码占用的80%的运行时间,20%的代码占用了80%的内存,20%的代码占用了80%的程序资源。
- 基于2-8准则,性能优化首先要确定程序热点,可借助vtune等性能分析工具。
- 性能优化最优先的方式:减少计算量(算法),其次再考虑代码层面及基于硬件的优化,例如:量化/cache/向量化等。
下面这些优化技巧,90%的情况下是无用的(感知不到性能的提升),因为硬件越来越牛逼了,除非热点,否则即使不优化,程序运行的速度也很快。为什么还要做呢? —— 苍蝇再小也是肉:)
TRICK-1. vector 使用reserve提前为对象分配内存
vector因简单方便,为大家所喜爱,使用频率非常高。但是,一旦使用不当,成为性能瓶颈的可能性也很高。在已经确定对象最大个数的前提下,有三种分配内存的方式:
- 声明并初始化-std::vector<obj> obj_vec(NUM_ELEMENT);
- 使用resize - obj_vec.resize(NUM_ELEMENT, default_value);
- 使用reserve - obi_vec.reserve(NUM_ELEMENT);
上述3种方式,哪个更好?答案:reserve。因为reserve减少了对象构造的次数,直接减少了工作量。我们来做个简单的测试:
#include <iostream>
#include <vector>
class ComplexClass { // 复杂类在构造时,需要更多资源(时间/内存等)
public:
ComplexClass() {
std::cout << "ComplexClass constructor" << std::endl;
}
};
int main()
{
// 构造10个对象,对象中成员变量为默认值
std::cout << "initialize vector with element number..." << std::endl;
std::vector<ComplexClass> vec1(5);
std::cout << "vec1 size:" << vec1.size() << std::endl << std::endl;
// 构造10个对象,对象中成员变量为默认值
std::cout << "vector resize..." << std::endl;
std::vector< // 构造10个对象> vec2;
vec2.resize(5);
std::cout << "vec2 size:" << vec2.size() << std::endl << std::endl;
//
std::cout << "vector reserve..." << std::endl;
std::vector<BigClass> vec3;
vec3.reserve(5);
std::cout << "vec3 size:" << vec3.size() << std::endl << std::endl;
system("pause");
return 0;
}
运行结果如下:
declaration and init with element num:
ComplexClass constructor
ComplexClass constructor
ComplexClass constructor
ComplexClass constructor
ComplexClass constructor
vec1 size: 5
vector resize:
ComplexClass constructor
ComplexClass constructor
ComplexClass constructor
ComplexClass constructor
ComplexClass constructor
vec2 size: 5
vector reserve:
vec3 size: 0
vec3 capacity: 5
从上例中可以得出以下结论:
- 使用元素个数初始化、resize函数,都会调用默认构造函数,产生默认对象。如果构造的类比较复杂,消耗的资源还是很可观的,而且默认对象一般是没用的,这就比较坑了。
- 使用reserve函数,同样分配了内存空间(capacity改变),但是没有调用默认构造函数,而是在真正赛数据时,再构造“有用的”对象。
注意使用条件:预知vector中需要存放的最大元素个数;存放元素为较复杂的对象,如果是内置类型,没什么影响。
TRICK-2. vector 使用clear+shrink_to_fit函数来正确的释放内存
对于数据量比较大的vector,用完之后主动释放是很必要的。(比较大是多大?把我问住了:))在c++11之前,通常使用swap来实现vector内存的释放。c++11提供了专门的函数shrink_to_fit,这种主动释放内存的做法,对程序性能是有提升的。原因可能是:内存释放,使得L1/L2 cache可以缓存其他有用的数据。(个人想法,欢迎讨论)
当使用cklear函数清理vector元素时,实际上只是清理了vector中的元素,使得vector的size变成了0,但是它的capacity并没有变成0,也就是说vector所占用的内存并没有被释放掉。既然内存没有释放,那就还可以访问到,看下面的例子:
#include<iostream>
#include<vector>
int main() {
std::vector<int> int_arr {1,2,3,4,5};
std::cout << "size of int_arr:" << int_arr.size() << std::endl; // size 为5
std::cout << "capacity of int_arr:" << int_arr.capacity() << std::endl; // capacity为5
int_arr.clear();
std::cout << "size of int_arr:" << int_arr.size() << std::endl; // size为0
std::cout << "capacity of int_arr:" << int_arr.capacity() << std::endl; // capacity为5
cout << "int_att.at(0) = " << *int_arr.data() << endl; // 1
system("pause");
return 0;
}
下面是使用clear+shrink_to_fit实现内存主动释放的例子:(优雅的实现)
#include<iostream>
#include<vector>
int main() {
std::vector<int> int_arr {1,2,3,4,5};
std::cout << "size of int_arr:" << int_arr.size() << std::endl; // size 为5
std::cout << "capacity of int_arr:" << int_arr.capacity() << std::endl; // capacity为5
int_arr.clear();
int_arr.shrink_to_fit();
std::cout << "size of int_arr:" << int_arr.size() << std::endl; // size为0
std::cout << "capacity of int_arr:" << int_arr.capacity() << std::endl; // capacity为0, 实现了内存释放
system("pause");
return 0;
}
TRICK-3. 使用emplace_back减少拷贝
c++11提供了emplace系列函数,代替之前的push/insert系列,具体为:insert->emplace/push_back->emplace_back/push_front->emplace_front。相比之前的push_back等函数,emplace系列的有点在于:减少一次元素拷贝,可以在函数参数中传入的对象构造所需的信息,进而直接构造对象。说的可能不明白,直接看示例:
#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 << "object constructed." << std::endl;
}
Person(Person&& other)
: name(std::move(other.name)), country(std::move(other.country)), year(other.year) {
std::cout << "object moved." << std::endl;
}
};
int main() {
std::map<int, Person> m;
// 下面两种插入元素的方法,建议使用emplace
std::cout << "map insert..." << std::endl;
m.insert(std::make_pair(1, Person("rongzhenlee", "china", 2020)));
std::cout << "map emplace..." << std::endl;
m.emplace(1, Person("rongzhenlee", "china", 2020));
std::vector<Person> v;
std::cout << "vector push_back..." << std::endl;
v.push_back(Person("rongzhenlee", "china", 2020));
std::vector<Person> v1;
std::cout << "vector emplace_back..." << std::endl;
v1.emplace_back("rongzhenlee", "china", 2020);
system("pause");
return 0;
}
运行结果:
map insert...
object constructed.
object moved. // 构造pair移动一次
object moved. // 插入map移动一次
map emplace...
插入map constructed.
I am being moved.
vector push_back...
I am being constructed.
I am being moved.
vector emplace_back...
I am being constructed.
作者:吴尼玛
链接:https://juejin.im/post/5d40f16ff265da03cc089721
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- 使用emplace系列函数,可以减少移动拷贝;
- 但是,要求结构体必须有对应的构造函数,又要多写几行代码;
TRICK-4. 使用无序容器-unordered系列
- c++11提供了无序容器,在没有排序需求的情况下(大多数情况用不到排序),优先使用无序容器。这一条比较好理解,相比有序容器,无序容器减少了排序的操作,所以效率会更高。
- 无序容器内部使用hashtable 的数据结构存储元素。通过哈希函数和关键字类型的 “==” 运算符来实现元素的快速操作。对于基本类型,我们可以像使用有序容器一样使用无序容器。对于自定义类型的结构体,就需要提供哈希函数和重载“==”运算符。示例:
#include <iostream>
#include <set>
#include <unordered_set>
#include <unordered_map>
using namespace std;
struct Node {
Node() {}
Node(int _x, int _y):x(_x), y(_y) {}
int x, y;
bool operator == (const Node &t) const {
return x==t.x && y==t.y;
}
};
struct NodeHash {
std::size_t operator () (const Node &t) const {
return t.x * 100 + t.y;
}
};
unordered_set <Node, NodeHash> h_set;
unordered_map <Node, string, NodeHash> h_map;
int main()
{
h_set.insert(Node(1, 2));
int x = 1, y = 2;
if(h_set.find(Node(x, y)) == h_set.end()) {
std::cout << "Not found" << std::endl;
}
else std::cout << "Found succeed" << std::endl; // 查找成功
h_map[Node(1, 2)] = "World";
std::cout << h_map[Node(1, 2)] << std::endl; // World
return 0;
}