我得到了以下用于读取 txt 文件并返回字典的方法。读取一个约 5MB 的文件(67000 行,每行 70 个字符)需要约 7 分钟。
public static Dictionary<string, string> FASTAFileReadIn(string file)
{
Dictionary<string, string> seq = new Dictionary<string, string>();
Regex re;
Match m;
GroupCollection group;
string currentName = string.Empty;
try
{
using (StreamReader sr = new StreamReader(file))
{
string line = string.Empty;
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith(">"))
{// Match Sequence
re = new Regex(@"^>(\S+)");
m = re.Match(line);
if (m.Success)
{
group = m.Groups;
if (!seq.ContainsKey(group[1].Value))
{
seq.Add(group[1].Value, string.Empty);
currentName = group[1].Value;
}
}
}
else if (Regex.Match(line.Trim(), @"\S+").Success &&
currentName != string.Empty)
{
seq[currentName] += line.Trim();
}
}
}
}
catch (IOException e)
{
Console.WriteLine("An IO exception has benn thrown!");
Console.WriteLine(e.ToString());
}
finally { }
return seq;
}
代码的哪一部分最耗时,如何加快速度?
谢谢