<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