473,418 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Counting lines in a file


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
1 1354
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jabailo | last post by:
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...
19
by: Alex Vinokur | last post by:
Is there any tool to count C-program lines except comments? Thanks, ===================================== Alex Vinokur mailto:alexvn@connect.to http://mathforum.org/library/view/10978.html...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
4
by: Peter | last post by:
Currently I'm using the method below, is there someting more efficient?: Imports System.IO Public Class CountLine Public Shared Function CountLines(ByVal FileName As String) As Integer Dim fs...
5
by: andy.lee23 | last post by:
hi im having trouble counting lines in a text file, i have the following code int node1, node2, i; char name; float value; ifstream fin; fin.open(OpenDialog1->FileName.c_str()); i=1;
7
by: Mark..... | last post by:
Hi, Can someone tell me the easiest way to count the number of lines in a text file? I can write a loop to do this but it seems cumbersome.... there must be an easier way?? Thanks in...
14
by: Dan | last post by:
Is this discouraged?: for line in open(filename): <do something with line> That is, should I do this instead?: fileptr = open(filename) for line in fileptr: <do something with line>
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
3
by: Robert | last post by:
I would like to count lines in a file using the fileinput module and I am getting an unusual output. ------------------------------------------------------------------------------...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.