ifstream, getline, and istringstream slow performance on big file in c++
<p>I want to ask about my code below: (The code below basically read an input file named inputVelocity.dat with format stated in the code. The code reads with istringstream to pass each value to each particular arrays)</p>

<pre><code>std::ifstream inputVelocity("input/inputVelocity.dat");
std::string lineInputVelocity;
while (std::getline(inputVelocity, lineInputVelocity))  {
  std::istringstream issVelocity(lineInputVelocity);
  double a, b, c, d, e;

  if (!(issVelocity &gt;&gt; a &gt;&gt; b &gt;&gt; c &gt;&gt; d &gt;&gt; e))  {
    std::cout &lt;&lt; "ISS ERROR" &lt;&lt; std::endl;
  }

  for (int k=0; k&lt;=nz+1; k++)  {
    for (int j=0; j&lt;=ny+1; j++)  {
      for (int i=0; i&lt;=nx+1; i++)  {
        ux[i][j][k]     =   a;
        uy[i][j][k]     =   b;
        uz[i][j][k]     =   c;
        pressure[i][j][k]   =   d;
        temperature[i][j][k]    =   e;
      }
    }
  }
}

inputVelocity.close();
</code></pre>

<p>The code is fine when reading around 20000 lines, but when I change the file into around 1.6 million lines, the code run very slow even on a server.</p>

<p>I did std::cout on each getline loop and it read like 5 lines/second, with around 1.6 million lines.</p>

<p>I have found some related questions here but still can't understand what's the problem source and how to fix it. Anyone can help?
Thank you.</p>