我的代码是:
int linenumber = File.ReadLines(path).Count();
但是大约 1 gig 大小的文件需要很长时间(大约 20 秒)。
那么有人知道解决这个问题的更好方法吗?
更新 6:
我已经测试了您的解决方案:
对于大约 870 mb 大小的文件:
方法一:{ my code time(seconds) : 13}
方法 2 : (from MarcinJuraszek & Locke) (同) {
time(seconds) : 12}
方法3:(来自Richard Deeming){ time(seconds) : 19}
方法4:(来自user2942249){ time(seconds) : 13}
方法 5 : (from Locke) {time(seconds) : 13是一样的lineBuffer = {4096 , 8192 , 16384 , 32768} }
方法 6 : (from Locke edition 2) { time(seconds) : 9 for Buffer size = 32KB, time(seconds) : 10 for Buffer size = 64KB }
正如我所说,在我的评论中,有一个应用程序(native code),可以在我的电脑中打开这个文件5 second。因此这是not about h.d.d speed.
By Compiling MSIL to Native Code,区别was not obvious。
Conclusion: 这时候,Locke method 2就是faster比其他方法。
所以我把他的帖子标记为Answer。但是如果有人的话,这篇文章将被打开find better idea。
vote up我为帮助我的亲爱的朋友+1 to solve the problem。
谢谢你的帮助。有趣的更好的主意。最好的问候聪明人