๐Ÿš€ FriesenByte

How to write a large buffer into a binary file in C fast

How to write a large buffer into a binary file in C fast

๐Ÿ“… | ๐Ÿ“‚ Category: C++

Penning ample buffers to binary records-data effectively is important for galore C++ purposes, peculiarly these dealing with information-intensive operations similar crippled improvement, advanced-show computing, and ample dataset processing. A poorly optimized penning procedure tin pb to important show bottlenecks, impacting the general responsiveness and person education. This article delves into assorted strategies and champion practices to speed up penning ample buffers to binary records-data successful C++, guaranteeing your purposes grip information with velocity and ratio.

Knowing Buffer Direction

Buffers enactment arsenic intermediaries betwixt your programme and the record scheme. They briefly shop information earlier it’s written to disk, decreasing the overhead of predominant disk entree. Knowing however buffers are managed is cardinal to optimizing record I/O operations. C++’s modular room offers respective buffering mechanisms, all with its ain commercial-offs. Selecting the correct 1 relies upon connected the circumstantial wants of your exertion. For case, utilizing unbuffered streams straight interacts with the working scheme for all compose cognition, starring to important overhead. Buffered streams, connected the another manus, accumulate information successful representation and execute less, bigger compose operations, bettering show.

Selecting the correct buffer measurement is besides important. Excessively tiny, and you’ll extremity ahead with extreme disk writes; excessively ample, and you’ll hazard consuming extreme representation. Experimentation and profiling are cardinal to uncovering the optimum buffer measurement for your circumstantial workload.

Using the C++ Modular Room

The C++ modular room provides almighty instruments for record I/O. The ofstream people offers a advanced-flat interface for penning to records-data. For binary record output, usage the ios::binary emblem once beginning the record. This ensures information is written precisely arsenic it’s represented successful representation, with out immoderate formatting oregon translations. Coupled with due buffer direction, ofstream gives a strong and businesslike manner to grip ample binary writes.

See this illustration:

c++ see see std::ofstream record(“output.bin”, std::ios::binary); std::vector buffer(1024 1024); // 1MB buffer // Enough buffer with information… record.compose(buffer.information(), buffer.dimension()); record.adjacent(); This codification snippet demonstrates penning a 1MB buffer to a binary record named “output.bin.” Retrieve that mistake dealing with and assets direction are important for exhibition-fit codification. Ever cheque for palmy record beginning and grip possible exceptions.

Boosting Show with Representation Mapping

Representation mapping supplies a much precocious and frequently quicker methodology for penning ample buffers, particularly once dealing with information that acceptable inside the addressable representation abstraction. It creates a nonstop mapping betwixt the record and a part of your programme’s representation. This eliminates the demand for express publication and compose operations, arsenic modifications to the mapped representation part are straight mirrored successful the record. Piece providing superior show, representation mapping requires cautious information of working scheme limitations and possible representation fragmentation points.

Piece the implementation particulars change crossed working techniques, the center conception entails mapping a record to a representation part, permitting nonstop entree and modification.

Asynchronous Penning

For eventual show successful I/O-sure functions, asynchronous penning permits your programme to proceed processing piece the penning cognition happens successful the inheritance. This concurrent attack tin importantly trim latency, peculiarly once dealing with precise ample information oregon aggregate concurrent compose operations. Contemporary C++ options similar std::early and std::async facilitate implementing asynchronous penning patterns effectively. Nevertheless, managing concurrent entree and guaranteeing information integrity requires cautious synchronization methods.

By overlapping computation with I/O operations, asynchronous penning retains your CPU engaged and maximizes throughput.

Optimizing for Coagulated Government Drives (SSDs)

If your mark retention is an SSD, see optimizing your penning scheme to leverage the alone traits of these units. SSDs excel astatine random entree and tiny artifact writes, truthful aligning your buffer dimension with the SSD’s artifact measurement tin additional better show. Seek the advice of your SSD’s documentation for circumstantial suggestions connected optimum artifact sizes. Moreover, beryllium conscious of compose amplification, a development that tin trim the lifespan of SSDs. Minimizing pointless compose operations contributes to the agelong-word wellness of your retention instrumentality.

  • Take the correct buffer measurement based mostly connected your exertion’s wants.
  • Make the most of representation mapping for show positive factors once due.
  1. Unfastened the record successful binary manner.
  2. Compose the buffer utilizing ofstream::compose.
  3. Adjacent the record.

For additional insights into precocious record I/O ideas, research sources similar cppreference and applicable Stack Overflow discussions.

Featured Snippet: To compose a ample buffer effectively successful C++, usage ofstream with ios::binary, see representation mapping for optimum show, and instrumentality asynchronous operations for concurrent processing. Due buffer sizing and alignment are important for maximizing throughput.

Larn much astir businesslike buffer dealing with.[Infographic Placeholder]

FAQ

Q: What is the champion buffer dimension for penning to binary records-data?

A: The optimum buffer measurement relies upon connected elements similar hardware, working scheme, and information traits. Experimentation and profiling are important to find the perfect dimension for your circumstantial exertion.

Effectively penning ample buffers is indispensable for advanced-show C++ purposes. By knowing buffer direction, leveraging the C++ modular room, and exploring precocious strategies similar representation mapping and asynchronous operations, you tin importantly better your information penning speeds. Retrieve to tailor your attack based mostly connected your circumstantial exertion’s necessities and mark hardware for most show good points. Research additional assets and experimentation with antithetic methods to good-tune your record I/O methods. See libraries similar Increase.Asio for precocious asynchronous operations and research less-flat scheme calls for equal finer-grained power complete your I/O operations if wanted. Steady profiling and benchmarking volition aid you place bottlenecks and additional optimize your penning processes. Increase C++ Libraries message precocious instruments for asynchronous operations, piece scheme calls similar compose() supply debased-flat power. Home windows API documentation offers blanket accusation connected record I/O for that level.

Question & Answer :
I’m making an attempt to compose immense quantities of information onto my SSD(coagulated government thrust). And by immense quantities I average 80GB.

I browsed the net for options, however the champion I got here ahead with was this:

#see <fstream> const unsigned agelong agelong dimension = 64ULL*1024ULL*1024ULL; unsigned agelong agelong a[measurement]; int chief() { std::fstream myfile; myfile = std::fstream("record.binary", std::ios::retired | std::ios::binary); //Present would beryllium any mistake dealing with for(int i = zero; i < 32; ++i){ //Any calculations to enough a[] myfile.compose((char*)&a,measurement*sizeof(unsigned agelong agelong)); } myfile.adjacent(); } 

Compiled with Ocular Workplace 2010 and afloat optimizations and tally nether Windows7 this programme maxes retired about 20MB/s. What truly bothers maine is that Home windows tin transcript records-data from an another SSD to this SSD astatine location betwixt 150MB/s and 200MB/s. Truthful astatine slightest 7 occasions quicker. That’s wherefore I deliberation I ought to beryllium capable to spell sooner.

Immoderate ideas however I tin velocity ahead my penning?

This did the occupation (successful the twelvemonth 2012):

#see <stdio.h> const unsigned agelong agelong dimension = 8ULL*1024ULL*1024ULL; unsigned agelong agelong a[measurement]; int chief() { Record* pFile; pFile = fopen("record.binary", "wb"); for (unsigned agelong agelong j = zero; j < 1024; ++j){ //Any calculations to enough a[] fwrite(a, 1, measurement*sizeof(unsigned agelong agelong), pFile); } fclose(pFile); instrument zero; } 

I conscionable timed 8GB successful 36sec, which is astir 220MB/s and I deliberation that maxes retired my SSD. Besides worthy to line, the codification successful the motion utilized 1 center a hundred%, whereas this codification lone makes use of 2-5%.

Acknowledgment a batch to everybody.

Replace: 5 years person handed it’s 2017 present. Compilers, hardware, libraries and my necessities person modified. That’s wherefore I made any adjustments to the codification and did any fresh measurements.

Archetypal ahead the codification:

#see <fstream> #see <chrono> #see <vector> #see <cstdint> #see <numeric> #see <random> #see <algorithm> #see <iostream> #see <cassert> std::vector<uint64_t> GenerateData(std::size_t bytes) { asseverate(bytes % sizeof(uint64_t) == zero); std::vector<uint64_t> information(bytes / sizeof(uint64_t)); std::iota(information.statesman(), information.extremity(), zero); std::shuffle(information.statesman(), information.extremity(), std::mt19937{ std::random_device{}() }); instrument information; } agelong agelong option_1(std::size_t bytes) { std::vector<uint64_t> information = GenerateData(bytes); car startTime = std::chrono::high_resolution_clock::present(); car myfile = std::fstream("record.binary", std::ios::retired | std::ios::binary); myfile.compose((char*)&information[zero], bytes); myfile.adjacent(); car endTime = std::chrono::high_resolution_clock::present(); instrument std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).number(); } agelong agelong option_2(std::size_t bytes) { std::vector<uint64_t> information = GenerateData(bytes); car startTime = std::chrono::high_resolution_clock::present(); Record* record = fopen("record.binary", "wb"); fwrite(&information[zero], 1, bytes, record); fclose(record); car endTime = std::chrono::high_resolution_clock::present(); instrument std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).number(); } agelong agelong option_3(std::size_t bytes) { std::vector<uint64_t> information = GenerateData(bytes); std::ios_base::sync_with_stdio(mendacious); car startTime = std::chrono::high_resolution_clock::present(); car myfile = std::fstream("record.binary", std::ios::retired | std::ios::binary); myfile.compose((char*)&information[zero], bytes); myfile.adjacent(); car endTime = std::chrono::high_resolution_clock::present(); instrument std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).number(); } int chief() { const std::size_t kB = 1024; const std::size_t MB = 1024 * kB; const std::size_t GB = 1024 * MB; for (std::size_t dimension = 1 * MB; dimension <= four * GB; dimension *= 2) std::cout << "option1, " << dimension / MB << "MB: " << option_1(dimension) << "sclerosis" << std::endl; for (std::size_t measurement = 1 * MB; measurement <= four * GB; dimension *= 2) std::cout << "option2, " << measurement / MB << "MB: " << option_2(measurement) << "sclerosis" << std::endl; for (std::size_t measurement = 1 * MB; dimension <= four * GB; measurement *= 2) std::cout << "option3, " << measurement / MB << "MB: " << option_3(measurement) << "sclerosis" << std::endl; instrument zero; } 

This codification compiles with Ocular Workplace 2017 and g++ 7.2.zero (a fresh necessities). I ran the codification with 2 setups:

  • Laptop computer, Center i7, SSD, Ubuntu sixteen.04, g++ Interpretation 7.2.zero with -std=c++eleven -march=autochthonal -O3
  • Desktop, Center i7, SSD, Home windows 10, Ocular Workplace 2017 Interpretation 15.three.1 with /Ox /Ob2 /Oi /Ot /GT /GL /Gy

Which gave the pursuing measurements (last ditching the values for 1MB, due to the fact that they had been apparent outliers): enter image description here enter image description here Some instances option1 and option3 max retired my SSD. I didn’t anticipate this to seat, due to the fact that option2 utilized to beryllium the quickest codification connected my aged device backmost past.

TL;DR: My measurements bespeak to usage std::fstream complete Record.