Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The only way to find the line count is to read the whole file and count the number of line-end characters. The fastest way tom do this is probably to read the whole file into a large buffer with one read operation and then go through the buffer counting the '\n' characters.</p> <p>As your current file size appears to be about 60Mb, this is not an attractive option. You can get some of the speed by not reading the whole file, but reading it in chunks., say of size 1Mb. You also say that a database is out of the question, but it really does look to be the best long-term solution.</p> <p><strong>Edit:</strong> I just ran a small benchmark on this and using the buffered approach (buffer size 1024K) seems to be a bit more than twice as fast as reading a line at a time with getline(). Here's the code - my tests were done with g++ using -O2 optimisation level:</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;vector&gt; #include &lt;ctime&gt; using namespace std; unsigned int FileRead( istream &amp; is, vector &lt;char&gt; &amp; buff ) { is.read( &amp;buff[0], buff.size() ); return is.gcount(); } unsigned int CountLines( const vector &lt;char&gt; &amp; buff, int sz ) { int newlines = 0; const char * p = &amp;buff[0]; for ( int i = 0; i &lt; sz; i++ ) { if ( p[i] == '\n' ) { newlines++; } } return newlines; } int main( int argc, char * argv[] ) { time_t now = time(0); if ( argc == 1 ) { cout &lt;&lt; "lines\n"; ifstream ifs( "lines.dat" ); int n = 0; string s; while( getline( ifs, s ) ) { n++; } cout &lt;&lt; n &lt;&lt; endl; } else { cout &lt;&lt; "buffer\n"; const int SZ = 1024 * 1024; std::vector &lt;char&gt; buff( SZ ); ifstream ifs( "lines.dat" ); int n = 0; while( int cc = FileRead( ifs, buff ) ) { n += CountLines( buff, cc ); } cout &lt;&lt; n &lt;&lt; endl; } cout &lt;&lt; time(0) - now &lt;&lt; endl; } </code></pre>
 

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