C++性能优化:提升代码执行效率
一、学习目标与重点
本章将深入探讨C++性能优化的核心知识,帮助你掌握提升代码执行效率的艺术。通过学习,你将能够:
介绍C++性能优化的核心知识,涵盖基本原则、分析工具、内存管理(智能指针、预分配)、CPU优化(循环合并、内联函数)、I/O优化及矩阵乘法综合案例。通过测量与对比,帮助开发者掌握提升代码执行效率的方法。

本章将深入探讨C++性能优化的核心知识,帮助你掌握提升代码执行效率的艺术。通过学习,你将能够:
性能优化应该遵循以下原则:
常用的性能分析工具包括:
#include <iostream>
#include <memory>
// 使用智能指针避免内存泄漏
class MyClass {
public:
MyClass() { std::cout << "MyClass 构造函数" << std::endl; }
~MyClass() { std::cout << "MyClass 析构函数" << std::endl; }
void doSomething() { std::cout << "MyClass 正在做某事" << std::endl; }
};
// 使用智能指针
void useSmartPointer() {
std::shared_ptr<MyClass> ptr = std::make_shared<MyClass>();
ptr->doSomething();
// 智能指针会自动释放内存,不需要手动调用 delete
}
// 手动管理内存(可能导致内存泄漏)
void useManualMemory() {
MyClass* ptr = new MyClass();
ptr->doSomething();
// 忘记调用 delete,导致内存泄漏
}
int main() {
std::cout << "=== 内存管理优化示例 ===" << std::endl;
std::cout << "使用智能指针:" << std::endl;
useSmartPointer();
std::cout << std::endl;
std::cout << "手动管理内存:" << std::endl;
useManualMemory();
return 0;
}
#include <iostream>
#include <vector>
// 预分配内存避免内存碎片
void preallocateMemory() {
const int size = 10000;
std::vector<int> vec;
vec.reserve(size); // 预分配内存
for (int i = 0; i < size; ++i) {
vec.push_back(i);
}
}
// 不预分配内存(可能导致内存碎片)
void notPreallocateMemory() {
const int size = 10000;
std::vector<int> vec;
for (int i = 0; i < size; ++i) {
vec.push_back(i);
}
}
int main() {
std::cout << "=== 内存碎片优化示例 ===" << std::endl;
// 测量预分配内存和不预分配内存的性能
auto start = std::chrono::high_resolution_clock::now();
preallocateMemory();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "预分配内存耗时: " << duration << "微秒" << std::endl;
start = std::chrono::high_resolution_clock::now();
notPreallocateMemory();
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "不预分配内存耗时: " << duration << "微秒" << std::endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
// 优化循环:合并操作
void optimizedLoop() {
const int size = 10000;
std::vector<int> vec1(size, 1);
std::vector<int> vec2(size, 2);
std::vector<int> result(size, 0);
for (int i = 0; i < size; ++i) {
result[i] = vec1[i] + vec2[i];
}
}
// 未优化的循环:多次遍历
void unoptimizedLoop() {
const int size = 10000;
std::vector<int> vec1(size, 1);
std::vector<int> vec2(size, 2);
std::vector<int> result(size, 0);
for (int i = 0; i < size; ++i) {
result[i] = vec1[i];
}
for (int i = 0; i < size; ++i) {
result[i] += vec2[i];
}
}
int main() {
std::cout << "=== 循环优化示例 ===" << std::endl;
// 测量优化循环和未优化循环的性能
auto start = std::chrono::high_resolution_clock::now();
optimizedLoop();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "优化循环耗时: " << duration << "微秒" << std::endl;
start = std::chrono::high_resolution_clock::now();
unoptimizedLoop();
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "未优化循环耗时: " << duration << "微秒" << std::endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
// 优化函数:使用内联函数
inline int add(int a, int b) {
return a + b;
}
// 未优化的函数:普通函数调用
int addNotInline(int a, int b) {
return a + b;
}
// 测试函数调用开销
void testFunctionCallOverhead() {
const int size = 1000000;
int result = 0;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < size; ++i) {
result += add(i, i);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "内联函数调用耗时: " << duration << "微秒" << std::endl;
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < size; ++i) {
result += addNotInline(i, i);
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "普通函数调用耗时: " << duration << "微秒" << std::endl;
}
int main() {
std::cout << "=== 函数优化示例 ===" << std::endl;
testFunctionCallOverhead();
return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
// 优化文件I/O:关闭同步以加速
void optimizedFileIO() {
const std::string filename = "test.txt";
const int size = 10000;
std::ofstream file(filename);
std::ios_base::sync_with_stdio(false); // 禁用同步以提高性能
for (int i = 0; i < size; ++i) {
file << i << std::endl;
}
file.close();
}
// 未优化的文件I/O:使用默认缓冲区
void unoptimizedFileIO() {
const std::string filename = "test.txt";
const int size = 10000;
std::ofstream file(filename);
for (int i = 0; i < size; ++i) {
file << i << std::endl;
}
file.close();
}
int main() {
std::cout << "=== 文件I/O优化示例 ===" << std::endl;
// 测量优化文件I/O和未优化文件I/O的性能
auto start = std::chrono::high_resolution_clock::now();
optimizedFileIO();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "优化文件I/O耗时: " << duration << "微秒" << std::endl;
start = std::chrono::high_resolution_clock::now();
unoptimizedFileIO();
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "未优化文件I/O耗时: " << duration << "微秒" << std::endl;
return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <boost/asio.hpp>
#include <chrono>
using boost::asio::ip::tcp;
using namespace std;
// 优化网络I/O:使用异步操作
void optimizedNetworkIO() {
try {
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query("example.com", "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
std::string request = "GET / HTTP/1.1\r\n";
request += "Host: example.com\r\n";
request += "Connection: close\r\n\r\n";
boost::asio::write(socket, boost::asio::buffer(request));
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");
string status_line;
istringstream response_stream(response.str());
response_stream >> status_line;
} catch (const std::exception& e) {
cerr << "错误:" << e.what() << endl;
}
}
// 未优化的网络I/O:使用同步操作
void unoptimizedNetworkIO() {
try {
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query("example.com", "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
std::string request = "GET / HTTP/1.1\r\n";
request += "Host: example.com\r\n";
request += "Connection: close\r\n\r\n";
boost::asio::write(socket, boost::asio::buffer(request));
string response;
char buffer[1024];
size_t len;
while ((len = socket.read_some(boost::asio::buffer(buffer))) > 0) {
response.append(buffer, len);
}
} catch (const std::exception& e) {
cerr << "错误:" << e.what() << endl;
}
}
int main() {
std::cout << "=== 网络I/O优化示例 ===" << std::endl;
// 测量优化网络I/O和未优化网络I/O的性能
auto start = std::chrono::high_resolution_clock::now();
optimizedNetworkIO();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "优化网络I/O耗时:" << duration << "毫秒" << std::endl;
start = std::chrono::high_resolution_clock::now();
unoptimizedNetworkIO();
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "未优化网络I/O耗时:" << duration << "毫秒" << std::endl;
return 0;
}
MatrixMultiplicationOptimization/
├── include/
│ └── Matrix.h
├── src/
│ ├── Matrix.cpp
│ └── main.cpp
└── build/
// include/Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <vector>
#include <chrono>
using namespace std;
using namespace chrono;
class Matrix {
public:
Matrix(int rows, int cols);
Matrix(const vector<vector<int>>& data);
int getRows() const;
int getCols() const;
int& operator()(int row, int col);
const int& operator()(int row, int col) const;
Matrix multiplyNaive(const Matrix& other) const;
Matrix multiplyOptimized(const Matrix& other) const;
void print() const;
vector<vector<int>> getTransposed() const; // 新增声明
private:
int rows_;
int cols_;
vector<vector<int>> data_;
};
#endif // MATRIX_H
// src/Matrix.cpp
#include "Matrix.h"
#include <iostream>
Matrix::Matrix(int rows, int cols) : rows_(rows), cols_(cols), data_(rows, vector<int>(cols, 0)) {}
Matrix::Matrix(const vector<vector<int>>& data) : rows_(data.size()), cols_(data[0].size()), data_(data) {}
int Matrix::getRows() const { return rows_; }
int Matrix::getCols() const { return cols_; }
int& Matrix::operator()(int row, int col) { return data_[row][col]; }
const int& Matrix::operator()(int row, int col) const { return data_[row][col]; }
Matrix Matrix::multiplyNaive(const Matrix& other) const {
if (cols_ != other.rows_) {
throw invalid_argument("矩阵尺寸不兼容");
}
Matrix result(rows_, other.cols_);
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < other.cols_; ++j) {
for (int k = 0; k < cols_; ++k) {
result(i, j) += data_[i][k] * other.data_[k][j];
}
}
}
return result;
}
Matrix Matrix::multiplyOptimized(const Matrix& other) const {
if (cols_ != other.rows_) {
throw invalid_argument("矩阵尺寸不兼容");
}
Matrix result(rows_, other.cols_);
vector<vector<int>> otherTransposed = other.getTransposed();
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < other.cols_; ++j) {
int sum = 0;
for (int k = 0; k < cols_; ++k) {
sum += data_[i][k] * otherTransposed[j][k];
}
result(i, j) = sum;
}
}
return result;
}
vector<vector<int>> Matrix::getTransposed() const {
vector<vector<int>> transposed(cols_, vector<int>(rows_));
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
transposed[j][i] = data_[i][j];
}
}
return transposed;
}
void Matrix::print() const {
for (const auto& row : data_) {
for (int value : row) {
cout << value << " ";
}
cout << endl;
}
}
// src/main.cpp
#include <iostream>
#include <vector>
#include <chrono>
#include "Matrix.h"
using namespace std;
using namespace chrono;
int main() {
std::cout << "=== 矩阵乘法优化示例 ===" << std::endl;
// 创建两个矩阵
const int size = 100;
Matrix matrix1(size, size);
Matrix matrix2(size, size);
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
matrix1(i, j) = i + j;
matrix2(i, j) = i * j;
}
}
// 测试朴素算法
auto start = high_resolution_clock::now();
Matrix resultNaive = matrix1.multiplyNaive(matrix2);
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start).count();
std::cout << "朴素算法耗时:" << duration << "毫秒" << std::endl;
// 测试优化算法
start = high_resolution_clock::now();
Matrix resultOptimized = matrix1.multiplyOptimized(matrix2);
end = high_resolution_clock::now();
duration = duration_cast<milliseconds>(end - start).count();
std::cout << "优化算法耗时:" << duration << "毫秒" << std::endl;
return 0;
}
# 创建构建目录
mkdir -p build && cd build
# 配置 CMake
cmake -DCMAKE_BUILD_TYPE=Release ..
# 编译项目
cmake --build . --config Release
# 运行程序
./MatrixMultiplicationOptimization
本章介绍了C++性能优化的核心知识,包括:

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML 转 Markdown 互为补充。 在线工具,Markdown 转 HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML 转 Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online