我得到了以下用於讀取txt文件並返回字典的方法。讀取〜5MB文件需要大約7分鐘(67000行,每行70個字符)。 如何加快此代碼?
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;
}
代碼的哪些部分是最耗時的,如何加快步伐?
感謝
相關:http://stackoverflow.com/questions/3927/what-are-some-good-net-profilers – 2012-07-24 03:05:33
@布萊恩,謝謝,這可以節省一些時間。 :) – sarnold 2012-07-24 03:05:49
不要每次都創建一個新的正則表達式。創建一次,並使用'RegexOptions.Compiled'標誌來獲得額外的性能。 – Ryan 2012-07-24 03:06:55