Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I speed up line by line reading of an ASCII file? (C++)
    primarykey
    data
    text
    <p>Here's a bit of code that is a considerable bottleneck after doing some measuring:</p> <pre><code>//----------------------------------------------------------------------------- // Construct dictionary hash set from dictionary file //----------------------------------------------------------------------------- void constructDictionary(unordered_set&lt;string&gt; &amp;dict) { ifstream wordListFile; wordListFile.open("dictionary.txt"); std::string word; while( wordListFile &gt;&gt; word ) { if( !word.empty() ) { dict.insert(word); } } wordListFile.close(); } </code></pre> <p>I'm reading in ~200,000 words and this takes about 240 ms on my machine. Is the use of <code>ifstream</code> here efficient? Can I do better? I'm reading about <code>mmap()</code> implementations but I'm not understanding them 100%. The input file is simply text strings with *nix line terminations.</p> <p><strong>EDIT: Follow-up question</strong> for the alternatives being suggested: Would <em>any</em> alternative (minus increasing the stream buffer sizes) imply that I write a parser that examines each character for new-lines? I kind of like the simple syntax of streams, but I can re-write something more nitty-gritty if I have to for speed. Reading the entire file in to memory is a viable option, it's only about 2mb.</p> <p><strong>EDIT #2:</strong> I've found that the slow down for me was due to the set insert, but for those who are still interested in speeding up line by line file IO, please read the answers here AND check out <a href="https://stackoverflow.com/questions/5166263/how-to-get-iostream-to-perform-better">Matthieu M.'s continuation on the topic.</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload