将文本文件读入字符串变量的最快方法是什么?
我理解它可以通过几种方式完成,比如读取单个字节,然后将它们转换为字符串。我在寻找一种编码最少的方法。
将文本文件读入字符串变量的最快方法是什么?
我理解它可以通过几种方式完成,比如读取单个字节,然后将它们转换为字符串。我在寻找一种编码最少的方法。
当前回答
@Cris抱歉,这是微软的MSDN
方法
在这个实验中,将对两个班级进行比较。StreamReader和FileStream类将被指示从应用程序目录中读取两个10K和200K的完整文件。
StreamReader (VB.NET)
sr = New StreamReader(strFileName)
Do
line = sr.ReadLine()
Loop Until line Is Nothing
sr.Close()
FileStream (VB.NET)
Dim fs As FileStream
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Dim b(1024) As Byte
fs = File.OpenRead(strFileName)
Do While fs.Read(b, 0, b.Length) > 0
temp.GetString(b, 0, b.Length)
Loop
fs.Close()
结果
在这个测试中,FileStream明显更快。StreamReader读取小文件需要额外50%的时间。对于大文件,它需要额外的27%的时间。
StreamReader是专门寻找换行符,而FileStream没有。这将占用一些额外的时间。
建议
根据应用程序需要对某段数据做什么,可能会有额外的解析,这将需要额外的处理时间。考虑这样一个场景,其中文件有数据列,行是CR/LF分隔的。StreamReader将沿着文本行查找CR/LF,然后应用程序将执行额外的解析以查找数据的特定位置。(你认为弦。子字符串没有价格?)
另一方面,FileStream以块的形式读取数据,积极主动的开发人员可以编写更多的逻辑来使用流。如果需要的数据位于文件中的特定位置,这当然是一种方法,因为它可以降低内存使用量。
FileStream在速度上是更好的机制,但需要更多的逻辑。
其他回答
对于那些觉得这个东西有趣和有趣的新手来说,在大多数情况下(根据这些基准测试)将整个文件读入字符串的最快方法是通过以下方法:
using (StreamReader sr = File.OpenText(fileName))
{
string s = sr.ReadToEnd();
}
//you then have to process the string
然而,绝对最快的读取文本文件的整体似乎是以下:
using (StreamReader sr = File.OpenText(fileName))
{
string s = String.Empty;
while ((s = sr.ReadLine()) != null)
{
//do what you have to here
}
}
与其他几种技术相比,它在大多数情况下胜出,包括与BufferedReader的竞争。
你也可以从文本文件中读取文本到字符串,如下所示
string str = "";
StreamReader sr = new StreamReader(Application.StartupPath + "\\Sample.txt");
while(sr.Peek() != -1)
{
str = str + sr.ReadLine();
}
File的基准比较。c#文件处理中的ReadAllLines vs StreamReader ReadLine
结果。StreamReader对于10000 +的大文件要快得多 行,但对于较小的文件,差异可以忽略不计。像往常一样, 计划不同大小的文件,并使用File。仅当 性能并不重要。
StreamReader方法
作为文件。ReadAllText方法已经被其他人建议,您也可以尝试更快的方法(我没有定量测试性能影响,但它似乎比File更快。ReadAllText(参见下面的比较))。不过,只有在较大的文件中才能看到性能上的差异。
string readContents;
using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
{
readContents = streamReader.ReadToEnd();
}
File.Readxxx()与StreamReader.Readxxx()的比较
通过ILSpy查看指示性代码,我发现了以下关于文件的内容。ReadAllLines File.ReadAllText。
文件。ReadAllText -使用StreamReader。ReadToEnd内部 文件。ReadAllLines -也使用StreamReader。内部ReadLine,额外的开销是创建List<string>作为读取行返回,并循环直到文件结束。
所以这两个方法都是建立在StreamReader之上的额外便利层。这从该方法的指示体中可以明显看出。
由ILSpy反编译的File.ReadAllText()实现
public static string ReadAllText(string path)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (path.Length == 0)
{
throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
}
return File.InternalReadAllText(path, Encoding.UTF8);
}
private static string InternalReadAllText(string path, Encoding encoding)
{
string result;
using (StreamReader streamReader = new StreamReader(path, encoding))
{
result = streamReader.ReadToEnd();
}
return result;
}
System.IO.StreamReader myFile =
new System.IO.StreamReader("c:\\test.txt");
string myString = myFile.ReadToEnd();
@Cris抱歉,这是微软的MSDN
方法
在这个实验中,将对两个班级进行比较。StreamReader和FileStream类将被指示从应用程序目录中读取两个10K和200K的完整文件。
StreamReader (VB.NET)
sr = New StreamReader(strFileName)
Do
line = sr.ReadLine()
Loop Until line Is Nothing
sr.Close()
FileStream (VB.NET)
Dim fs As FileStream
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Dim b(1024) As Byte
fs = File.OpenRead(strFileName)
Do While fs.Read(b, 0, b.Length) > 0
temp.GetString(b, 0, b.Length)
Loop
fs.Close()
结果
在这个测试中,FileStream明显更快。StreamReader读取小文件需要额外50%的时间。对于大文件,它需要额外的27%的时间。
StreamReader是专门寻找换行符,而FileStream没有。这将占用一些额外的时间。
建议
根据应用程序需要对某段数据做什么,可能会有额外的解析,这将需要额外的处理时间。考虑这样一个场景,其中文件有数据列,行是CR/LF分隔的。StreamReader将沿着文本行查找CR/LF,然后应用程序将执行额外的解析以查找数据的特定位置。(你认为弦。子字符串没有价格?)
另一方面,FileStream以块的形式读取数据,积极主动的开发人员可以编写更多的逻辑来使用流。如果需要的数据位于文件中的特定位置,这当然是一种方法,因为它可以降低内存使用量。
FileStream在速度上是更好的机制,但需要更多的逻辑。