Home >Backend Development >C++ >How to optimize C++ I/O operations to improve performance?
To improve C I/O performance, several approaches can be taken: Use buffered I/O to group data to reduce the number of disk accesses. Use the mmap() system call to map files directly into memory to avoid frequent disk accesses. Use parallel I/O to perform I/O operations simultaneously on multiple threads or processes to increase throughput.
How to optimize C I/O operations to improve performance
I/O operations are critical to the performance of your application. In C, there are several ways to optimize I/O operations to improve performance.
1. Using Buffered I/O
Buffered I/O involves grouping data into large chunks and then writing or reading them from disk. This reduces the number of disk accesses, thereby improving performance.
#include <iostream> #include <fstream> #include <vector> int main() { std::vector<int> data(1000000); std::ofstream file("data.bin", std::ios::binary); // 缓冲 1 MB 的数据 file.rdbuf()->pubsetbuf(nullptr, 1024 * 1024); // 写入数据 file.write((char*)&data[0], data.size() * sizeof(int)); file.close(); return 0; }
2. Using mmap()
The mmap() system call allows you to map files directly into memory. This avoids frequent disk accesses, thus improving performance.
#include <sys/mman.h> #include <fcntl.h> int main() { // 打开文件 int fd = open("data.bin", O_RDWR); // 将文件映射到内存 void* data = mmap(nullptr, 1000000 * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); // 操作数据 ... // 取消映射 munmap(data, 1000000 * sizeof(int)); close(fd); return 0; }
3. Using Parallel I/O
Parallel I/O involves performing I/O operations on multiple threads or processes simultaneously. This can improve throughput and reduce overall execution time.
#include <thread> #include <vector> int main() { std::vector<std::thread> threads; for (int i = 0; i < 4; i++) { threads.emplace_back([] { // 执行 I/O 操作 }); } for (auto& thread : threads) { thread.join(); } return 0; }
Practical case
The following is a practical case using C to optimize I/O operations. This program reads and writes large amounts of data from a file:
#include <iostream> #include <fstream> #include <vector> #include <chrono> using namespace std; int main() { // 数据量 const int dataSize = 1000000; // 使用缓冲 I/O { vector<int> data(dataSize); ofstream file("data.bin", ios::binary); file.rdbuf()->pubsetbuf(nullptr, 1024 * 1024); // 记录时间 auto start = chrono::high_resolution_clock::now(); // 写入数据 file.write((char*)&data[0], data.size() * sizeof(int)); auto end = chrono::high_resolution_clock::now(); // 计算执行时间 auto duration = chrono::duration_cast<chrono::milliseconds>(end - start); cout << "Buffered I/O duration: " << duration.count() << " ms" << endl; } // 使用 mmap() { vector<int> data(dataSize); int fd = open("data.bin", O_RDWR); void* dataPtr = mmap(nullptr, dataSize * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); // 记录时间 auto start = chrono::high_resolution_clock::now(); // 写入数据 memcpy(dataPtr, &data[0], data.size() * sizeof(int)); auto end = chrono::high_resolution_clock::now(); // 取消映射 munmap(dataPtr, dataSize * sizeof(int)); close(fd); // 计算执行时间 auto duration = chrono::duration_cast<chrono::milliseconds>(end - start); cout << "mmap() duration: " << duration.count() << " ms" << endl; } return 0; }
Run this program and you'll notice that using mmap() is many times faster than buffered I/O.
The above is the detailed content of How to optimize C++ I/O operations to improve performance?. For more information, please follow other related articles on the PHP Chinese website!