最快的方式来获得文本文件中的某些行 [英] Fastest way to get certain lines from text file

查看:81
本文介绍了最快的方式来获得文本文件中的某些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有一定结构的一些文本文件。在每行的开始我有例如真或假字部分。事情是这样的:

I have some text files that have a certain structure. At the start of each row I have some word for example "true" or "false". Something like this:

$True 3 5 1 8
$False 12 5 a z
$False 1,5,7 
$True 123
$True 7ao
$False 198

我需要得到一个数组,列表或类似的与价值观的内容:

I need to get an array, list or something like that with values:

$False 12 5 a z
$False 1,5,7 
$False 198

行数不知道,我想找到一个特定的启动字(假),以只读行的最快方法。
我尝试使用 string.Split 方法,然后加入列表解析如果Word ==字,但问题在速度,如果我有5-6000此行变得缓慢。是否有这样的事情更快的方法?

The number of rows is not known, I am trying to find the fastest way to read only rows with a certain start word (False). I tried to parse using the string.Split method, and then adding to list if word == word, but problem is in speed, if I have 5-6000 lines this becomes slow. Are there faster way for something like this?

推荐答案

这应该是足够快的速度:

This should be fast enough:

var filteredLines = 
        File.ReadLines("path")
            .Where(line => line.StartsWith(word))
            .ToList()

readlines方法

becase ReadLines iterates through file line by line, not loading whole into memory (from MSDN):

方法不同作为readlines方法和ReadAllLines如下:当您使用readlines方法,可以在返回整个集合之前开始枚举字符串的集合;当您使用ReadAllLines,你必须等待串全阵列之前,您可以访问阵列返回。因此,当您处理非常大的文件时,readlines方法可以更有效。

The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.

这篇关于最快的方式来获得文本文件中的某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆