473,659 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 21 '05 #1
6 1706
Hi Javailo,

I would guess that option c scales better, but a small test might show
the real winner ;-)

Ward

ja*****@texeme. com wrote:
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

Jul 21 '05 #2
I haven't tested but probably A for any arbitrary length of file.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<ja*****@texeme .com> wrote in message
news:Nf******** ************@sp eakeasy.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


--
Texeme Textcasting Technology
http://www.texeme.com

Jul 21 '05 #3
Hi,

I think (b) should be the fastest, but I wouldn't expect any significant
difference between (a) and (b) since Windows does efficient disk read
buffering behind the scenes.
I think (c) should be the slowest because of RegExp.

Anyway, it's all just guessing, why don't you try all the three approaches
and post the real result here? ;-)

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
<ja*****@texeme .com> wrote in message
news:Nf******** ************@sp eakeasy.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


--
Texeme Textcasting Technology
http://www.texeme.com


Jul 21 '05 #4
Hi,

Giving my guess as well A/B ex aquo as first.

:-)

Cor
Jul 21 '05 #5
"ja*****@texeme .com" <ja*****@texeme .com> wrote in
news:Nf******** ************@sp eakeasy.net:
Which would be faster for counting lines in a StreamReader:
The only way to find out is to test it, and in fact different ones may be faster depending on size and
characteristics of the file.
(a) iterate through a file using .ReadLine() and adding to a counter,
i++
This is probably the best over all considering memory considerations etc if your file is large. And
probably the fastest too.
(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


Both of these will consume large amoutns of memory if the file is large.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
Jul 21 '05 #6
hi,

the first option is the way to go, unless you are 100% sure that the size of
the file in advance.

The others solutions are not portable, you should use
Environment.New Line() instead of "\r\n"

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<ja*****@texeme .com> wrote in message
news:Nf******** ************@sp eakeasy.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


--
Texeme Textcasting Technology
http://www.texeme.com

Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
5685
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 news://news.gmane.org/gmane.comp.lang.c++.perfometer =====================================
1
1367
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 to count the occurances of \r\n (c) doing a .ReadToEnd() and using a RegEx to count the number of occurances of \r\n
1
6916
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 with uploaded MS WORD .doc files. Using code like that below: strLine = sr.ReadLine While Not IsNothing(strLine) 'Not eof If Trim(strLine) <> "" Then 'Not blank
4
1809
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 As IO.FileStream Dim sr As IO.StreamReader Dim Result As Integer Try fs = New IO.FileStream( _
5
7683
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
13484
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 advance
14
2076
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
2095
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 lines. and after that save both of those files... since that is a big block of text (15000 lines) i thint that it is a big job to look for a keyword... so my question is this exactly: how do i do this:
3
3589
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. ------------------------------------------------------------------------------ #!/usr/bin/python import fileinput # cycle through files for line in fileinput.input(): if (fileinput.isfirstline()): if (fileinput.lineno 1):
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.