Writing Fast to Disk in C++

Chris
1 min readSep 27, 2021

A quick and dirty performance test of a variety of file APIs

Your mileage will vary depending on your hardware. Tests were performed using a mechanical disk but the underlying principles should be consistent with most drives. Each test program creates a single vector containing a large amount of data which “random” chunks between 1byte and 1kilobyte are then selected until 1GB has been written, this may represent your usage well, it may not, but it is relatively consistent. The original purpose was to simulate something that looked similar to video data being streamed from a camera. The code is available here.

The Simple Approach(7659ms)

Open the file with std::ofstream and call its write() method.

The fopen Approach(6613ms)

Open the file with fopen() and call the fwrite() function.

The open approach(2656ms)

Open the file with open() and then call the write() function.

The Direct Approach(1337ms)

Similar to above but use the O_DIRECT flag and package the data into page size chunks.

Discord Patreon Reddit Monocle Github Twitter

--

--