Home >Backend Development >C++ >How to Achieve Optimal File Writing Performance in C ?

How to Achieve Optimal File Writing Performance in C ?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-11 14:22:23588browse

How to Achieve Optimal File Writing Performance in C  ?

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>
<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><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*)&amp;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(&amp;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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn