By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

Counting lines in a file

P: n/a

Which would be faster for counting lines in a StreamReader:

(a) iterate through a file using .ReadLine() and adding to a counter, i++

(b) doing a .ReadToEnd() and then using an IndexOf() method to count the
occurances of \r\n

(c) doing a .ReadToEnd() and using a RegEx to count the number of occurances
of \r\n


--
Texeme Textcasting Technology
http://www.texeme.com
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
<ja*****@texeme.com> wrote in message news:Nf********************@speakeasy.net...
Which would be faster for counting lines in a StreamReader:

(a) iterate through a file using .ReadLine() and adding to a counter, i++

(b) doing a .ReadToEnd() and then using an IndexOf() method to count the
occurances of \r\n

(c) doing a .ReadToEnd() and using a RegEx to count the number of occurances
of \r\n


It depends. Is the StreamReader layered on top of a MemoryStream that's
entirely in-memory? is it layered over a FileStream referring to a resource
on a UNC share being retrieved over the Ethernet? or is it layered on top
of a NetworkStream where the remainder of the Stream's content is going
to have some high latency associated with its packets?

If the entire Stream's contents are available, then (b) is usually going to
be faster for large Streams, because it does all I/O immediately at the
beginning. OTOH, if the call to ReadToEnd( ) is going to block (as in
the case with a network socket) then certainly (a) is going to be faster.
(a) will starting accumulating the number of lines as the received Stream,
and on it's very last read it has very little work to do whereas at that
point, (b) is only getting started.

(c) is generally slower, primarily because a RegEx is a general-purpose
pattern matching routine which is overpowered for this problem (i.e.,
there already exist simpler Framework methods that scan directly to
these characters w/o a RegEx's initial overhead).
Derek Harmon
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.