How to Enhance File Writing Performance in C
When writing voluminous data to a binary file, optimizing performance is crucial. Here's how to enhance your writing speed:
Utilizing FILE* for Direct File Handling:
In the example code, the use of FILE* allows direct file access, bypassing intermediate layers and reducing overhead. This approach yields significant performance gains, as observed in the original question.
Code Optimization for FILE* Usage:
#include <stdio.h><br>const unsigned long long size = 8ULL<em>1024ULL</em>1024ULL;<br>unsigned long long a[size];</p> <p>int main()<br>{</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">FILE* pFile; pFile = fopen("file.binary", "wb"); for (unsigned long long j = 0; j < 1024; ++j){ // Some calculations to fill a[] fwrite(a, 1, size*sizeof(unsigned long long), pFile); } fclose(pFile); return 0;
}
This optimized code utilizes FILE* directly to write to the binary file, achieving faster writing speeds.
Comparison of Approaches:
Recent measurements indicate that std::fstream offers comparable performance to FILE* for large file writing. Here's a comparison of different approaches:
#include <fstream></p><h1>include <chrono></h1><h1>include <vector></h1><h1>include <cstdint></h1><h1>include <numeric></h1><h1>include <random></h1><h1>include <algorithm></h1><h1>include <iostream></h1><h1>include <cassert></h1><p>long long option_1(std::size_t bytes)<br>{</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// Using std::fstream std::vector<uint64_t> data = GenerateData(bytes); auto startTime = std::chrono::high_resolution_clock::now(); auto myfile = std::fstream("file.binary", std::ios::out | std::ios::binary); myfile.write((char*)&data[0], bytes); myfile.close(); auto endTime = std::chrono::high_resolution_clock::now(); return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
}
long long option_2(std::size_t bytes)
{
// Using FILE* std::vector<uint64_t> data = GenerateData(bytes); auto startTime = std::chrono::high_resolution_clock::now(); FILE* file = fopen("file.binary", "wb"); fwrite(&data[0], 1, bytes, file); fclose(file); auto endTime = std::chrono::high_resolution_clock::now(); return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
}
Measurements show that both option_1 (std::fstream) and option_2 (FILE*) achieve comparable performance for large file writing.
Conclusion:
For writing large buffers to a binary file in C , both FILE* and std::fstream offer high performance. The choice between the two depends on specific requirements and preferences.
The above is the detailed content of How to Achieve Optimal File Writing Performance in C ?. For more information, please follow other related articles on the PHP Chinese website!