473,320 Members | 1,940 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,320 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
Jul 21 '05 #1
6 1663
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********************@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


--
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********************@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


--
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********************@speakeasy.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/
"Programming 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.NewLine() 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********************@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


--
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
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: 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...
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.