模板Lambda加速C++性能
Lambda 不仅是语法糖,更是性能优化利器!
引言
Daniel Lemire 是著名的性能研究员,本文介绍如何使用模板 Lambda 显著提升 C++ 性能。
🎯 核心观点:Lambda 不仅是语法糖,更是性能优化利器!
1. 模板Lambda基础
普通Lambda vs 模板Lambda
1 2 3 4 5 6 7 8 9
| auto add = [](int a, int b) { return a + b; }; add(1, 2);
auto tadd = []<typename T>(T a, T b) { return a + b; }; tadd(1, 2); tadd(1.0, 2.0); tadd(1L, 2L);
|
使用示例
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <vector> #include <numeric>
auto sum = []<typename T>(const std::vector<T>& v) { return std::accumulate(v.begin(), v.end(), T{}); };
std::vector<int> vi = {1, 2, 3, 4, 5}; std::vector<double> vd = {1.0, 2.0, 3.0};
sum(vi); sum(vd);
|
2. 编译期多态 vs 运行时多态
传统虚函数(运行时多态)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Processor { public: virtual double process(double x) = 0; };
class FastProcessor : public Processor { public: double process(double x) override { return x * 2.0; } };
Processor* p = new FastProcessor(); double result = p->process(3.14);
|
模板Lambda(编译期多态)
1 2 3 4 5 6
| auto fast_processor = []<typename T>(T x) { return x * T(2.0); };
double result = fast_processor(3.14);
|
性能对比
| 实现 |
调用开销 |
内联可能性 |
| 虚函数 |
高 |
否 |
| 函数指针 |
中 |
困难 |
| Lambda |
无 |
是 |
3. SIMD 加速
手动 SIMD
1 2 3 4 5 6 7 8 9 10
| #include <immintrin.h>
void add_arrays(double* a, double* b, double* c, size_t n) { for (size_t i = 0; i < n; i += 4) { __m256d va = _mm256_load_pd(a + i); __m256d vb = _mm256_load_pd(b + i); __m256d vc = _mm256_add_pd(va, vb); _mm256_store_pd(c + i, vc); } }
|
4. 实际案例:高性能过滤
性能测试结果
| 方法 |
耗时 |
相对性能 |
| copy_if |
1.0x |
基准 |
| 手写循环 |
0.6x |
1.7x |
| 模板Lambda |
0.4x |
2.5x |
5. Lambda 捕获优化
按值 vs 按引用
1 2 3 4 5 6 7 8 9 10 11 12 13
| std::vector<double> large_data(100000); auto f = [=]() { return large_data[0]; };
std::vector<double> large_data(100000);
auto f = [&large_data]() { return large_data[0]; };
auto* ptr = large_data.data(); auto g = [ptr]() { return ptr[0]; };
|
6. 最佳实践
使用指南
- 优先使用模板 Lambda 处理多种类型
- 避免捕获大型对象,使用指针或引用
- 保持 Lambda 简短 以便内联
- 使用
constexpr 在编译期计算
1 2 3 4 5 6 7 8
| constexpr auto factorial = []<int N>() { int result = 1; for (int i = 2; i <= N; ++i) result *= i; return result; };
constexpr int f5 = factorial(5);
|
总结
| 特性 |
传统Lambda |
模板Lambda |
| 类型安全 |
✅ |
✅ |
| 泛型能力 |
❌ |
✅ |
| 内联优化 |
有限 |
完全 |
| SIMD |
困难 |
容易 |
本文档基于 Daniel Lemire 研究扩展而成