Efficiently Writing Large Buffers to Binary Files in C
Writing substantial amounts of data to a binary file can be challenging, especially when seeking high performance. This article investigates techniques to accelerate this process in C .
Performance Bottlenecks
The initial approach, utilizing std::fstream, achieved only 20MB/s. This disparity with the considerably faster file copy speeds on the same SSD suggested potential optimizations.
SOLUTION A: Switching to C-style File Handling
The use of C-style file handling (FILE*) significantly improved performance, achieving speeds of approximately 220MB/s. This method bypassed intermediate layers and directly interacted with the operating system's file system, reducing overhead.
SOLUTION B: Disabling Stream Synchronization
Disabling stream synchronization with std::ios_base::sync_with_stdio(false) noticeably enhanced the performance of the std::fstream approach. This modification eliminated unnecessary operations that hindered data writing.
Comparing Options
Recent benchmarks reveal that both std::fstream (with stream synchronization disabled) and C-style file handling can effectively max out the write speed of an SSD. However, std::fstream provides a simpler and more modern API, while C-style file handling offers slightly better performance in certain scenarios.
CONCLUSION
For writing large buffers to binary files, the following strategies are recommended:
The above is the detailed content of How Can I Efficiently Write Large Buffers to Binary Files in C ?. For more information, please follow other related articles on the PHP Chinese website!