跳到主要内容C++ 性能优化:提升代码执行效率 | 极客日志C++算法
C++ 性能优化:提升代码执行效率
综述由AI生成C++ 性能优化涵盖内存管理、CPU 技巧及 I/O 操作。通过智能指针、预分配内存、循环合并、内联函数及异步 I/O 等手段提升效率。矩阵乘法案例展示了算法优化策略。
FlinkHero5 浏览 C++ 性能优化:提升代码执行效率

学习目标与重点
本文深入探讨 C++ 性能优化的核心知识,帮助你掌握提升代码执行效率的方法。通过学习,你将能够:
- 理解性能优化的基本概念,掌握性能分析的方法
- 学会优化内存管理,减少内存泄漏和内存碎片
- 理解 CPU 优化技巧,提高代码的执行速度
- 学会优化 I/O 操作,提升文件和网络读写的效率
- 培养性能优化思维,设计高效的代码
性能优化的基本概念
性能优化的原则
性能优化应该遵循以下原则:
- 先测量后优化:在优化之前,必须先测量代码的性能,找出瓶颈所在
- 优化瓶颈:只优化对性能影响最大的部分
- 保持代码的可维护性:优化后的代码应该易于理解和维护
- 测试优化结果:优化后必须测试代码的正确性和性能提升效果
性能分析工具
常用的性能分析工具包括:
- GProf:GNU 的性能分析工具
- Valgrind:内存调试和性能分析工具
- Perf:Linux 下的性能分析工具
- Visual Studio Profiler:Windows 下的性能分析工具
内存管理优化
内存泄漏的检测与修复
#include <iostream>
#include <memory>
class MyClass {
public:
MyClass() {
std::cout << "MyClass 构造函数" << std::endl;
}
~MyClass() {
std::cout << "MyClass 析构函数" << std::endl;
}
void doSomething() {
std::cout << << std::endl;
}
};
{
std::shared_ptr<MyClass> ptr = std::<MyClass>();
ptr->();
}
{
MyClass* ptr = ();
ptr->();
}
{
std::cout << << std::endl;
std::cout << << std::endl;
();
std::cout << std::endl;
std::cout << << std::endl;
();
;
}
"MyClass 正在做某事"
void useSmartPointer()
make_shared
doSomething
void useManualMemory()
new
MyClass
doSomething
int main()
"=== 内存管理优化示例 ==="
"使用智能指针:"
useSmartPointer
"手动管理内存:"
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;
}
CPU 优化技巧
循环优化
#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;
}
I/O 操作优化
文件 I/O 优化
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
void optimizedFileIO() {
const std::string filename = "test.txt";
const int size = 10000;
std::ofstream file(filename);
file.rdbuf()->pubsetbuf(nullptr, 0);
for (int i = 0; i < size; ++i) {
file << i << std::endl;
}
file.close();
}
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;
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;
}
网络 I/O 优化
#include <iostream>
#include <vector>
#include <string>
#include <boost/asio.hpp>
#include <chrono>
using boost::asio::ip::tcp;
using namespace std;
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);
response_stream >> status_line;
} catch (const std::exception& e) {
cerr << "错误:" << e.what() << endl;
}
}
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;
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/
核心代码
#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
#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;
}
}
#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 -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release
./MatrixMultiplicationOptimization
总结与练习
本章总结
- 性能优化的基本概念与原则
- 内存管理优化
- CPU 优化技巧
- I/O 操作优化
- 综合案例:优化矩阵乘法算法
练习题
- 写一个程序,使用 GProf 或 Perf 分析代码的性能。
- 编写一个函数,使用智能指针避免内存泄漏。
- 写一个程序,使用内存对齐优化代码的性能。
- 实现一个类,使用循环优化提升代码的执行速度。
- 写一个程序,使用异步 I/O 提升文件读写的效率。
进阶挑战
- 研究如何使用 C++ 的 SIMD 指令集优化循环。
- 学习如何使用 C++ 的并发编程优化 CPU 密集型任务。
- 研究如何使用 C++ 的内存池优化内存管理。
- 学习如何使用 C++ 的缓存优化技术提升代码的性能。
- 研究如何使用 C++ 的 JIT 编译技术优化代码的执行速度。
相关免费在线工具
- 加密/解密文本
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
- Gemini 图片去水印
基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online
- Base64 字符串编码/解码
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
- Base64 文件转换器
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
- Markdown转HTML
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
- HTML转Markdown
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online