现代C++性能优化实战(上)
移动语义、智能指针、Lambda、并行算法详解
引言
本文介绍现代 C++(C++11/14/17/20/23)中的性能优化技术,这些技术使得代码既安全又高效。
🎯 核心理念:现代 C++ 的设计让高性能与安全性可以兼得!
1. 移动语义 (Move Semantics)
避免不必要的拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| std::vector<int> getData() { std::vector<int> v; return v; }
std::vector<int> getData() { std::vector<int> v; return v; }
return std::move(v);
|
移动 vs 拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Buffer { char* data_; size_t size_; public: Buffer(Buffer&& other) noexcept : data_(other.data_), size_(other.size_) { other.data_ = nullptr; other.size_ = 0; } Buffer& operator=(Buffer&& other) noexcept { if (this != &other) { delete[] data_; data_ = other.data_; size_ = other.size_; other.data_ = nullptr; other.size_ = 0; } return *this; } };
|
2. 完美转发 (Perfect Forwarding)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| template<typename T> void processValue(T v) { doProcess(v); }
template<typename T> void processValue(T&& v) { doProcess(std::forward<T>(v)); }
processValue(42); int x = 42; processValue(x); processValue(std::move(x));
|
3. 智能指针与内存管理
unique_ptr
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Widget { Resource* resource_; public: ~Widget() { delete resource_; } };
class Widget { std::unique_ptr<Resource> resource_; public: };
auto widget = std::make_unique<Widget>();
|
shared_ptr 与 weak_ptr
1 2 3 4 5 6 7 8 9
| auto sp1 = std::make_shared<Widget>(); auto sp2 = sp1;
class Node { std::shared_ptr<Node> next; std::weak_ptr<Node> parent; };
|
4. Lambda 表达式优化
捕获策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| int main() { int value = 42; auto f1 = [=]() { return value; }; auto f2 = [&]() { return value; }; auto f3 = [=, &value]() { value++; }; auto f4 = [this]() { return member_; }; }
|
性能考虑
1 2 3 4 5 6 7 8 9
| std::vector<int> largeData(1000000, 1); auto f = [=]() { return largeData[0]; };
std::vector<int> largeData(1000000, 1); auto f = [&largeData]() { return largeData[0]; };
auto f = [data = std::move(largeData)]() { return data[0]; };
|
5. 容器优化
vector 预分配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| std::vector<int> v; for (int i = 0; i < 1000; ++i) { v.push_back(i); }
std::vector<int> v; v.reserve(1000); for (int i = 0; i < 1000; ++i) { v.push_back(i); }
std::vector<std::pair<int, std::string>> v; v.emplace_back(1, "hello");
|
6. 并行算法 (C++17)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <algorithm> #include <execution>
std::vector<int> data(1000000);
std::sort(data.begin(), data.end());
std::sort(std::execution::par, data.begin(), data.end());
std::for_each(std::execution::par, data.begin(), data.end(), [](int& x) { x = x * 2; });
std::transform(std::execution::par, src.begin(), src.end(), dst.begin(), [](int x) { return x * 2; });
std::reduce(std::execution::par, data.begin(), data.end(), 0);
|
7. constexpr 与编译时计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| constexpr int factorial(int n) { return n <= 1 ? 1 : n * factorial(n - 1); }
int arr[factorial(5)];
constexpr auto add = [](int a, int b) constexpr { return a + b; };
constexpr std::array<int, 5> arr = {1, 2, 3, 4, 5}; constexpr auto sum = std::accumulate(arr.begin(), arr.end(), 0);
|
总结
| 技术 |
优势 |
适用场景 |
| 移动语义 |
避免拷贝 |
返回大型对象 |
| 完美转发 |
通用参数 |
模板库 |
| unique_ptr |
自动内存管理 |
独占资源 |
| Lambda |
代码内联 |
短回调 |
| 并行算法 |
多核加速 |
大数据处理 |
| constexpr |
编译时计算 |
常量计算 |
本文档基于 drlongnecker 现代C++性能优化系列扩展而成