I would like to know if concurrent file writing is effective.
Short answer: physical recording to the same disk from several streams at the same time will not be faster than writing to this disk from a single stream (here we are talking about normal hard drives). In some cases, it can even be much slower.
But, as always, it depends on many factors:
OS Cache: Records are usually cached by the operating system and then written to disk in chunks. Thus, multiple threads can write to this cache at the same time without any problems and have an advantage in speed. Especially if processing / preparing data takes longer than writing to disk.
In some cases, even when directly writing to a physical disk from several threads, the OS optimizes this and writes only large blocks to each file.
However, in the worst case, smaller blocks can be written to disk every time, which leads to the need to search for a hard disk (Β± 10 ms on a regular hdd!) On each file switch (with the same on an SSD it will not be so bad, because there is more direct access and no need to search).
Thus, in the general case, when writing to a disk from several streams at the same time, it may be a good idea to prepare (some) data in memory and write the final data to disk in large blocks using some kind of lock or, possibly, from one dedicated write-thread . If files grow during recording (i.e. the file size is not set in front), writing data to larger blocks can also prevent disk fragmentation (at least as much as possible).
On some systems, there may not be any difference whatsoever, but on others it can make a big difference and become much slower (or even on the same system with different hard drives).
In order to well appreciate the differences in recording speed using one stream or several streams, the total file sizes would have to be larger than the available memory - or at least all the buffers should be flushed to disk before measuring the end time, measuring only time, which is required to write data to the OS cache does not make much sense here.
Ideally, the total time taken to write all the data to the disk should equal the write speed of the physical hard disk. If writing to a disc using one stream is slower than the speed of writing to the disk (this means that processing the data takes longer than writing), it is obvious that using more streams will speed up the process. If recording from several streams becomes slower than the speed of writing to the disk, time will be lost in accessing the disk caused by switching between different files (or different blocks inside one large file).
To get an idea of ββthe time loss when performing a large number of disk accesses, let's look at some numbers:
Let's say we have hdd with a write speed of 50 MB / s:
Recording a single continuous block of 50 MB will take 1 second (under ideal conditions).
Performing the same in 1 MB blocks, with a file switch and resulting disk access between them, will give: 20 ms for recording the search time 1 MB + 10 ms. Writing 50 MB will take 1.5 seconds. which is 50% more time, only for a quick search between them (the same is true for reading from disk) - the difference will be even greater, given the higher read speed.
In reality, it will be somewhere in the middle, depending on the system.
Although we could hope that the OS takes care of all this (or, for example, using IOCP ), this is not always the case.