How to increase file reading speed?
<p>I have large .txt files with more than a million lines and 7 colums of float numbers per line. The columns are seperated via spaces.</p>

<p>Currently, I import the files by reading each line (getline), transforming the line to a stream and then storing the seven values into array variables (please see my minimal example). However, this procedure is quite slow and takes around 10 minutes for 3 million lines (500MB). This corresponds to 0.8 MB/s and is much slower than it takes to write the files. My hard drive is SSD.</p>

<p>Can you give me advice of how to improve the efficiency of the code?</p>

<p>Bests, Fabian</p>

<p><strong>C++</strong></p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;

struct Container { double a, b, c, d, e, f, g; };

void read_my_file(std::ifstream &amp;file, Container *&amp;data) {
    std::string line;
    std::stringstream line_as_stream;
    unsigned int column;
    unsigned long int row;

    data = new Container[300000]; //dynamically allocated because the 
                                  //length is usually a user input.

    for (row = 0; row &lt; 300000; row++) {
        getline(file, line);
        line_as_stream.str(line);

        for (column = 0; column &lt; 7; column++) {
            line_as_stream &gt;&gt; data[row].a;
            line_as_stream &gt;&gt; data[row].b;
            line_as_stream &gt;&gt; data[row].c;
            line_as_stream &gt;&gt; data[row].d;
            line_as_stream &gt;&gt; data[row].e;
            line_as_stream &gt;&gt; data[row].f;
            line_as_stream &gt;&gt; data[row].g;
        }

        line_as_stream.clear();
    }
}

int main(void) {
    Container *data = nullptr;
    std::ifstream file;

    file.open("./myfile.txt", std::ios::in);
    read_my_file(file, data);
    std::cout &lt;&lt; data[2].b &lt;&lt; "\n";

    file.close();

    return 0;
}
</code></pre>