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
Release: 2024-12-11 14:22:23
Original
573 people have browsed it

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>
<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;
Copy after login

}

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*)&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();
Copy after login

}

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();
Copy after login

}

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!

source:php.cn
Previous article:How Do I Call Base Class Constructors in C ? Next article:When Should I Use `const` vs. `constexpr` for Variables?
Statement of this Website
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template