I've been searching in the web about this question and although there are many similar questions about read/write in C/C++, I haven't found about this specific task.
I want to be able to read from multiple files (256x256 files) only sizeof(double)
bytes located in a certain position of each file. Right now my solution is, for each file:
Open the file (read, binary mode):
fstream fTest("current_file", ios_base::out | ios_base::binary);
Seek the position I want to read:
fTest.seekg(position*sizeof(test_value), ios_base::beg);
Read the bytes:
fTest.read((char *) &(output[i][j]), sizeof(test_value));
And close the file:
fTest.close();
This takes about 350 ms
to run inside a for{ for {} }
structure with 256x256 iterations (one for each file).
Q: Do you think there is a better way to implement this operation? How would you do it?
If possible, I suggest reorganizing the data. For example, put all those doubles into one file instead of spreading them across multiple files.
If you need to run the program multiple times and the data doesn't change, you may want to create a tool that will optimize the data first.
The performance issue with the files is the overhead of:
In most file based systems, that use a lot of data, reading data is optimized to have a longer duration than any overhead. The requests would be cached and sorted for optimal disk access. Unfortunately, in your case, you are not reading enough data so that the overhead is now longer duration than the reading.
I suggest trying to queue the reading operation of the data. Take 4 threads, each opens a file and reads the doubles, then places them into a buffer. The idea here is stagger the operations.
Hopefully, these threads can keep the hard drive busy enough to not slow down; continuous activity. You may be able to try this in a single thread first. If you need better performance, you may want to consider sending commands directly to disk drive (order them first).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With