计算文件中的行数 [英] Counting lines in a file
本文介绍了计算文件中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
计算StreamReader中的行数会更快:
(a)使用.ReadLine()迭代文件并添加到counter,i ++
(b)执行.ReadToEnd()然后使用IndexOf()方法计算
出现的\\\ n $
(c)做一个.ReadToEnd()并使用RegEx来计算出现次数
of \\\\ n >
-
Texeme Textcasting Technology
http://www.texeme.com
解决方案
嗨Javailo,
>
我猜这个选项c会更好地扩展,但是一个小测试可能会显示
真正的赢家;-)
Ward >
ja*****@texeme.com 写道:哪个会更快地计算StreamReader中的行数:
(a)迭代一个文件使用.ReadLine()并添加到一个计数器,i ++
(b)做一个.ReadToEnd(),然后用一个IndexOf()方法来计算\的出现\ (c)做一个.ReadToEnd()并使用RegEx计算出现次数
of \\ n
我没有测试过,但是对于任意长度的文件可能都是A.
-
鲍勃鲍威尔[MVP]
Visual C#,System.Drawing
在Windows窗体中查找精彩的Windows窗体提示和技巧
< a rel =nofollowhref =http://www.bobpowell.net/tipstricks.htmtarget =_ blank> http://www.bobpowell.net/tipstricks.htm
使用GDI +常见问题解答回答那些GDI +问题
http://www.bobpowell.net/faqmain.htm
所有新文章都提供C#和VB.NET代码。
订阅到了R提供SS提要,绝不会错过任何新文章。
< ja ***** @ texeme.com>在消息中写道
新闻:Nf ******************** @ speakeasy.net ...<在StreamReader中计算行数会更快:
(a)使用.ReadLine()迭代文件并添加到计数器,i ++
( b)执行.ReadToEnd()然后使用IndexOf()方法来计算\\\\ n的发生情况
(c)执行.ReadToEnd()和使用RegEx计算
出现的数量
-
Texeme Textcasting Technology
http://www.texeme.com
我认为(b)应该是最快的,但我不希望任何重大的
之间的差异( a)和(b)因为Windows在幕后进行高效的磁盘读取
缓冲。
我认为(c)应该是最慢的,因为RegExp。
无论如何,这只是猜测,为什么不尝试所有三种方法
并在此发布真实结果? ;-)
-
此致,
Dmytro Lapshyn [Visual Developer - Visual C#MVP]
< ja ***** @ texeme.com>在消息中写道
新闻:Nf ******************** @ speakeasy.net ...<在StreamReader中计算行数会更快:
(a)使用.ReadLine()迭代文件并添加到计数器,i ++
( b)执行.ReadToEnd()然后使用IndexOf()方法来计算\\\\ n的发生情况
(c)执行.ReadToEnd()和使用RegEx计算
出现的数量
-
Texeme Textcasting Technology
http://www.texeme.com
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
解决方案
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
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
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
这篇关于计算文件中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文