I'm trying to read and write a few megabytes of data stored in files, consisting out of 8 floats converted to strings per line, to my SSD. Looking up C++ code and implementing some of the answers here for reading and writing files yielded me this code for reading a file:
std::stringstream file;
std::fstream stream;
stream.open("file.txt", std::fstream::in);
file << stream.rdbuf();
stream.close();
And this code for writing files:
stream.write(file.str().data(), file.tellg());
The problem is, that this code is very slow, compared to the speed of my SSD. My SSD has a reading speed of 2400 MB/s
and a writing speed of 1800 MB/s
.
But my program has a read speed of only 180.6 MB/s
and a write speed of 25.11 MB/s
.
Because some asked how I measure the speed, I obtain a std::chrono::steady_clock::time_point
using std::chrono::steady_clock::now()
and then do a std::chrono::duration_cast
.
Using the same 5.6MB large file and dividing the file size by the measured time, I get the megabytes per second.
How can I increase the speed of reading and writing to files, while using only standard C++ and STL?