title: Speed up integer reading from file in C++
body: <p>I'm reading a file, line by line, and extracting integers from it. Some noteworthy points:</p>
  
  <ul>
  <li>the input file is not in binary;</li>
  <li>I cannot load up the whole file in memory;</li>
  <li><p>file format (only integers, separated by some delimiter):</p>
  
  <pre><code>x1 x2 x3 x4 ...
  y1 y2 y3 ...
  z1 z2 z3 z4 z5 ...
  ...
  </code></pre></li>
  </ul>
  
  <p><em>Just to add context</em>, I'm reading the integers, and counting them, using an <code>std::unordered_map&lt;unsigned int, unsinged int&gt;</code>.</p>
  
  <p>Simply looping through lines, and allocating useless stringstreams, like this:</p>
  
  <pre><code>std::fstream infile(&lt;inpath&gt;, std::ios::in);
  while (std::getline(infile, line)) {
      std::stringstream ss(line);
  }
  </code></pre>
  
  <p>gives me ~2.7s for a 700MB file.</p>
  
  <p>Parsing each line:</p>
  
  <pre><code>unsigned int item;
  std::fstream infile(&lt;inpath&gt;, std::ios::in);
  while (std::getline(infile, line)) {
      std::stringstream ss(line);
      while (ss &gt;&gt; item);
  }
  </code></pre>
  
  <p>Gives me ~17.8s for the same file.</p>
  
  <p>If I change the operator to a <code>std::getline</code> + <code>atoi</code>:</p>
  
  <pre><code>unsigned int item;
  std::fstream infile(&lt;inpath&gt;, std::ios::in);
  while (std::getline(infile, line)) {
      std::stringstream ss(line);
      while (std::getline(ss, token, ' ')) item = atoi(token.c_str());
  }
  </code></pre>
  
  <p>It gives ~14.6s.</p>
  
  <p>Is there anything faster than these approaches? I don't think it's necessary to speed up the file reading, just the parsing itself -- both wouldn't make no harm, though (:</p>